change range of data

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
hi everyone,
ive recorded a simple macro and it works great although it has hardcoded the range of data ("D2:D1321")
how can i change this so its dynamic to different numbers of rows?



Code:
Sub Inventory_Report()
'
' Inventory_Report Macro
'

'
    Columns("A:B").EntireColumn.Hidden = True
    Columns("F:F").EntireColumn.Hidden = True
    Columns("K:L").EntireColumn.Hidden = True
    Columns("Q:Q").EntireColumn.AutoFit
    Columns("R:R").EntireColumn.Hidden = True
    Columns("T:AC").EntireColumn.Hidden = True
    Columns("AD:AD").EntireColumn.AutoFit
    Columns("AE:AE").EntireColumn.Hidden = True
    Columns("D:D").EntireColumn.AutoFit
    
    'Sort Data
    ActiveWorkbook.Worksheets("Inventory").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Inventory").Sort.SortFields.Add2 Key:=Range( _
        "D2:D1321"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Inventory").Sort
        .SetRange Range("A1:AE1321")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("C1").AutoFilter  
   
End Sub

thanks
Sam
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about
VBA Code:
   With Sheets("Inventory")
      .Range("A1:AE" & .Range("A" & Rows.Count).End(xlUp).Row).Sort .Range("D1"), xlAscending, , , , , , xlYes
   End With
This uses col A to find the last row.
 
Upvote 0
Solution
How about
VBA Code:
   With Sheets("Inventory")
      .Range("A1:AE" & .Range("A" & Rows.Count).End(xlUp).Row).Sort .Range("D1"), xlAscending, , , , , , xlYes
   End With
This uses col A to find the last row.

thank you this is perfect
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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