VBA code for last row instead of naming a range

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi please can you help me, i have the code below where i have named a range A28:C41 but the last row might not always be C41 please can you advise how i do a last row please?
Code:
With ActiveSheet.Sort
     .SortFields.Add Key:=Range("A28"), Order:=xlAscending
     .SortFields.Add Key:=Range("B28"), Order:=xlAscending
     .SetRange Range("A28:C41")
     .Header = xlYes
     .Apply
End With
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi

Try this:
Code:
lastrow = ActiveSheet.Cells(.Rows.Count, "A").End(xlUp).Row
With ActiveSheet.Sort      
     .SortFields.Add Key:=Range("A28"), Order:=xlAscending
     .SortFields.Add Key:=Range("B28"), Order:=xlAscending
     .SetRange Range("A28:C"&lastrow)
     .Header = xlYes
     .Apply
End With
 
Last edited:
Upvote 0
hi thank you for the help, i have tried this but i i get an error on the (.Rows bit on the 1st line, hope you can advise
 
Upvote 0
Sorry - I copied it from one of my workbooks and didn't amend it fully. Try this instead:
Code:
lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
 
Upvote 0
brilliant thank you for the help again, i dont know what i would do without all your help, i am still learning this :) but enjoyijng it as well :)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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