Solved

Removing absolute references in VBA Sort?

Posted on 2014-10-29
5
119 Views
Last Modified: 2014-10-30
How do I remove the absolute references in this code?  The  "C3:C179" and ("B3:C179") the selection is dynamic and will be different each time.

Sub Macro3()
'
' Macro3 Macro
'

'
    ActiveWorkbook.Worksheets("MatlTracker").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("MatlTracker").Sort.SortFields.Add Key:=Range( _
        "C3:C179"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("MatlTracker").Sort
        .SetRange Range("B3:C179")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B1:C1").Select
End Sub

Open in new window

0
Comment
Question by:RWayneH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40410719
Hi,

pls try

    ActiveWorkbook.Worksheets("MatlTracker").Sort.SortFields.Clear
    lastRow = Range("C" & Rows.Count).End(xlUp).Row
    ActiveWorkbook.Worksheets("MatlTracker").Sort.SortFields.Add Key:=Range( _
        "C3:C" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("MatlTracker").Sort
        .SetRange Range("B3:C" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B1:C1").Select

Open in new window

Regards
0
 
LVL 4

Expert Comment

by:yuppydu
ID: 40412806
Why don't you just assign a dynamic range name to the range?

i.e in the range name define give a name to the range (DataRange, for example) and set the range as OFFSET(B3,0,1,COUNTA($c:$c)) focus worksheet

Then the code  is .SetRange Range("DataRange")
0
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40412838
i like Rgonzo1971's approach on the solution.
0
 

Author Comment

by:RWayneH
ID: 40413077
I agree, Rgonzo1971 is a good one.  Thanks.
0
 

Author Closing Comment

by:RWayneH
ID: 40413078
Thanks for the help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question