How to run the same macro separately in all worksheets

Ramadan2512

Board Regular
Joined
Sep 7, 2024
Messages
68
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook with multiple sheets and I have a simple macro to go to the last row in the sheet
VBA Code:
Sub LastRow()

    Range("amberville").Select
    Selection.End(xlDown).Select

End Sub
my problem is that when i copy the macro button to other sheet in the same workbook it doesn't work unless I change the range name "amberville" but when i do this the button in the other sheet change automatically to the new range name

please let me know how to make the same macro run separately in each sheet
 
Last edited by a moderator:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Firstly, you can combine your code down to a single line of code, like this:
VBA Code:
Sub LastRow()
    Range("amberville").End(xlDown).Select
End Sub

Secondly, are you really looking for the last row on the sheet, or in your named range?
Are there different named ranges on every sheet?
If so, how is the code to know that the range name for a particular sheet is? Is there any logic to it?

If you truly want to find the last row on each sheet, if there is always data in column A for every row with data, you could simply use something like this:
VBA Code:
Sub LastRow()
    Cells(Rows.Count, "A").End(xlUp).EntireRow.Select
End Sub
which is generic enough to use on every sheet, so you could just have this code once, in a General module, and run it from any sheet.
 
Upvote 0
Firstly, you can combine your code down to a single line of code, like this:
VBA Code:
Sub LastRow()
    Range("amberville").End(xlDown).Select
End Sub

Secondly, are you really looking for the last row on the sheet, or in your named range?
Are there different named ranges on every sheet?
If so, how is the code to know that the range name for a particular sheet is? Is there any logic to it?

If you truly want to find the last row on each sheet, if there is always data in column A for every row with data, you could simply use something like this:
VBA Code:
Sub LastRow()
    Cells(Rows.Count, "A").End(xlUp).EntireRow.Select
End Sub
which is generic enough to use on every sheet, so you could just have this code once, in a General module, and run it from any sheet.
thank you so much for your reply - actually i have 9 worksheets in the workbook and each one has a data in table named "amverville" , "carnell" , "westridge"..etc...
I have tried your last code but it gave me the first row in the sheet and when i changed this line Cells(Rows.Count, "A").End(xlUp).EntireRow.Select to be .End(xlDown) it worked but it gives me the last row in the sheet not in the table

can you please fix it to be in table not in the entire sheet

thank you in advance
 
Upvote 0
Is your table in the same place on every sheet?
What cell does it start in (the upper, left-most cell address)?
 
Upvote 0
Give this a try:
VBA Code:
Sub LastRow()

    ActiveSheet.ListObjects(1).ListColumns(1).Range.End(xlDown).Select

End Sub
 
Upvote 1
Solution
Give this a try:
VBA Code:
Sub LastRow()

    ActiveSheet.ListObjects(1).ListColumns(1).Range.End(xlDown).Select

End Sub
Yes all tables are the same except only the data inside
now it works perfectly thank you soooooooooooo much Alex Blakenburg I do appreciate your qucik help
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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