hanz753

Board Regular
Joined
Aug 9, 2017
Messages
53
Hello,

I have this code and I would like it to stop at the last row of the data in each sheet.

Option Explicit
Sub LoopThroughSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

ActiveSheet.[d1:d33] = ActiveSheet.Name



On Error Resume Next
ws.Range("d2:d33") = ws.Name



Next ws
End Sub


Can this be done?

Kind Regards
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Re: How to change code to dynamic range

Code:
Sub LoopThroughSheets()
Dim ws As Worksheet, lr&
ActiveSheet.[D1] = ActiveSheet.Name
For Each ws In ActiveWorkbook.Worksheets
    lr = ws.Cells.Find(What:="*", After:=ws.[A1], _
        searchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ws.Range(ws.[D2], ws.Cells(lr, "D")) = ws.Name
Next
End Sub
 
Upvote 0
Re: How to change code to dynamic range

That's nearly perfect thank you.

But, what it does on the first sheet I need it to apply to all sheets starting from cell D1.

Thank you for your help
 
Upvote 0
Re: How to change code to dynamic range

Code:
Sub LoopThroughSheets()
Dim ws As Worksheet, lr&
For Each ws In ActiveWorkbook.Worksheets
    lr = ws.Cells.Find(What:="*", After:=ws.[A1], _
        searchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ws.Range(ws.[D1], ws.Cells(lr, "D")) = ws.Name
Next
End Sub
 
Upvote 0
Re: How to change code to dynamic range

Hi,

How would you apply this code 2 rows above the bottom of the data?
 
Upvote 0
Re: How to change code to dynamic range

Code:
Sub LoopThroughSheets()
Dim ws As Worksheet, lr&
For Each ws In ActiveWorkbook.Worksheets
    lr = ws.Cells.Find(What:="*", After:=ws.[A1], _
        searchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [COLOR=#ff0000]- 2[/COLOR]
    ws.Range(ws.[D1], ws.Cells(lr, "D")) = ws.Name
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,973
Members
449,200
Latest member
Jamil ahmed

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