Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removing absolute references in VBA Sort?

Posted on 2014-10-29
5
Medium Priority
?
122 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 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

670 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