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!
 
Yep, i tried to run the code by skipping those two lines and it seemed to have sorted everything fine. but i couldn't tell for sure as i had about 200 rows data.

So, i was just wondering if we has to have the selection made before using the custome sorting. Thanks for the answers guys!
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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