how to define variable row count in macros?

ansvk1

Board Regular
Joined
Oct 6, 2017
Messages
82
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi there,
I would to ask for a little help with learning how to change my row values to a variable while i was trying to customise/write this VBA code (from recorded macro) to suit my needs.

here is the
Rich (BB code):
Sub Macro2()


    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Clear
    'now sorting for Levles from 4 to 1
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("N2:N189" & xlDown??), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    'now sorting for Responsible Parties
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("T2:T189" & xlDown??), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub

i am trying to change the selection part form N2:N189 to N2:NxlDown (so that it selcts however many row are present later on). How can i do that?
Thanks in advance!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try
Code:
Sub Macro2()

    [COLOR=#0000ff]Dim UsdRws As Long

    UsdRws = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1").Select[/COLOR]
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Clear
    'now sorting for Levles from 4 to 1
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("[COLOR=#0000ff]N2:N" & UsdRws[/COLOR]), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    'now sorting for Responsible Parties
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("[COLOR=#0000ff]T2:T" & UsdRws[/COLOR]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort
        .header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub
 
Upvote 0
Try
Code:
Sub Macro2()

    [COLOR=#0000ff]Dim UsdRws As Long

    UsdRws = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
     [/COLOR]
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("[COLOR=#0000ff]N2:N" & UsdRws[/COLOR]), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    'now sorting for Responsible Parties
    ActiveWorkbook.Worksheets("Summary").AutoFilter.Sort.SortFields.Add Key:= _
        Range("[COLOR=#0000ff]T2:T" & UsdRws[/COLOR]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal

Worked just the same as the other suggestion, another interesting way to do it... thanks for the suggestion Fluff!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
A quick question on both suggestions.

Do i have to have my code lines below to make this macro run correctly?

Code:
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

in order for the excel to do the custome sorting correctly (that i used) to record this macro. Or, it doesn't matter and i can just discar the above 2 lines of code as well? Please let me know.

thanks!
 
Upvote 0
@ ansvk1:

You are welcome.

There are many ways to get the last used row.

The one that I suggested finds the last used row in a specific column.

Fluff's suggestion finds the last used row in the entire worksheet.
 
Upvote 0
I suspect that you can get rid of them, but the easiest way to find out, is try it. :)
 
Upvote 0
I second Fluff's comment.

You do not use the selection in your subsequent code. So, those two lines can be removed.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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