Solved

Removing absolute references in VBA Sort?

Posted on 2014-10-29
5
110 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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:ProfessorJimJam
Comment Utility
i like Rgonzo1971's approach on the solution.
0
 

Author Comment

by:RWayneH
Comment Utility
I agree, Rgonzo1971 is a good one.  Thanks.
0
 

Author Closing Comment

by:RWayneH
Comment Utility
Thanks for the help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel 2013 Problem 12 44
Excel VBA - UserForm control click 12 35
Help with Journey Roadmap Graphic 11 47
Splitting out Data 14 27
What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now