VBA to past sheet name in A2 of the same sheet in multiple sheets workbook

Sinuhet

New Member
Joined
Feb 9, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi
I have a workbook with 240 worksheets.
Name of each worksheet is a date with format "dd.mm.yyyy".
Each worksheet has an empty column A.

With the attached VBA I am able to correctly place "Date" in A1 of all sheets.
The minimum I want to place the name of each sheet in A2 of the sheet the name originates from.
However my code paste a name of active worksheet to all sheets, which is not what I want to do.
I want each sheet has in A2 its own sheet name.
How should I replace "activeSheet.Name" to achieve this?

If it is not a problem, could you also help me how to then modify the VBA code with the following logic:
After placing "Date" in A1 of all sheets, place name of the worksheet in cell range A2 -AX, where X is the last row where CX has a value (aka CX+1) is first empty cell in C1 to CX+1 range.
(I have chosen column C as column B has some empty cells before the end of the data range; each sheet has a different number of rows).

Thank you very much for the help with at lest the first part of my query!

VBA Code:
Sub myscript()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ws.Range("A1").Value = "Date"
    ws.Range("A2").Value = activeSheet.Name
    Next ws
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You need to use
VBA Code:
ws.Range("A2").Value = ws.Name
 
Upvote 0
thank you very much. Any chance to help me with the 2nd part, aka how to fill "ws.Name" in each sheet in the range from A2-Ax, so long A2-Ax range of that sheet is not empty excel cell? (all sheets have consecutive data from C2-Cx, where x differs between sheets)
 
Upvote 0
Missed that bit. Try
VBA Code:
Sub myscript()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ws.Range("A1").Value = "Date"
    ws.Range("A2:A" & ws.Range("C" & Rows.Count).End(xlUp).Row).Value = ws.Name
    Next ws
End Sub
 
Upvote 0
Solution
It worked out. Thank you very much and have a nice weekend!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

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