VBA reference sheet name dynamically

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
I have the following issue:
1) I need to get the last row used in each worksheet (I have this working)
2) I need to add the last row used value to a master sheet.

The master sheet looks like:

Sheet namedata range
Sheet 1$A$2:$J$5
Sheet 2$A$2:$J$10

<tbody>
</tbody>

"$A$2:$J$" will remain static - with the last row value added at the end for each sheet.

I would like to add a formula to compute the data ranges.
So far I have:

Code:
LR = Cells(Rows.Count, "A").End(xlUp).Row

For cl = 2 To LR


Cells(cl, 2).Value = "$A$2:$J$" & Sheets([B]"Sheet 1"[/B]).Cells(Rows.Count, "A").End(xlUp).Row


Next cl

which works fine for the first data record (i.e. Sheet 1 -> $A$2:$J$5). However, it obviously will put the same data range beside Sheet 2 (instead of $A$2:$J$10).
I would like to be able to replace the "Sheet 1" and insert something dynamic so that when the code jumps to the next line it automatically changes from referencing "Sheet 1" to "Sheet 2".

Thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Just figured it out by adding Sheetname = Cells(cl, 1).Value
 
Upvote 0
How about
Code:
Sub anglais428()
   Dim i As Long
   
   For i = 1 To Worksheets.Count
      With Worksheets(i)
         Sheets("Master").Range("A" & i + 1) = .Name
         Sheets("Master").Range("B" & i + 1) = "$A$2:$J$" & .Range("A" & Rows.Count).End(xlUp).Row
      End With
   Next i
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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