Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Removing absolute references in VBA Sort?

Posted on 2014-10-29
5
Medium Priority
?
123 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
5 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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 27

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

916 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