change range of data

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
996
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
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,682
Office Version
  1. 365
Platform
  1. Windows
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.
 
Solution

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
996
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,682
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,859
Messages
5,627,294
Members
416,237
Latest member
Tethys

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
Top