Creating a variable row for sorting in a macro

triplel1977

New Member
Joined
Oct 12, 2008
Messages
23
I am a newbie in macros, so I am basically using the recorder to create them.

I am dipping my fingers into editing, so my current problem is, how to edit the code so, regardless of the number of rows in a column it will sort all of them.

If I am reading the code correctly, he sample below says the range is going to sort to row N30, but next week I may have 150 rows. How do I write it for relative versus absolute (did I get that verbiage right?)

Thanks in advance.

Sample

ActiveWorkbook.Worksheets("BICSI_Billing").Sort.SortFields.Add Key:=Range( _
"N2:N30"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("BICSI_Billing").Sort
.SetRange Range("A1:W30")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try

Code:
Dim LR As Long
LR = Worksheets("BICSI_Billing").Range("N" & Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets("BICSI_Billing").Sort.SortFields.Add Key:=Range( _
"N2:N" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("BICSI_Billing").Sort
    .SetRange Range("A1:W" & LR)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Upvote 0
Thanks

I downloaded a test file with more rows

It appears to work, after it hit an error and I deleted the code for removing duplicate Invoice #s in column 10 and then I reran it. So I think I'm good.

Code Removed:

ActiveSheet.Range ("$A$1:$W$30").RemoveDuplicates Columns:=10, Header:=x1Yes

It goes back to the same type of problem on a different column. I need to get educated on this. Any recommendations?
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,254
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top