Solved

Removing absolute references in VBA Sort?

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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.

732 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