Copy worksheet name to a column of cells

For Excel 2016, I need a macro that will populate all cells in a single column with the name of the worksheet.  Only the cells where there is other data in the row would be populated with the worksheet name.  Blank cells in the column would remain blank.  This would apply to the multiple worksheets in the workbook.
Steve LowryPrincipal ChemistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
Steve

Is there a particular column the sheet name should go in?
0
Steve LowryPrincipal ChemistAuthor Commented:
Column B will work.  But any column that you designate, I can work with.
0
NorieVBA ExpertCommented:
Try this, it should put the sheet name in column B wherever there is a value in column A.
Dim ws As Worksheet
Dim rng As Range
Dim evl As Variant

    For Each ws In ActiveWorkbook.Sheets
        Set rng = ws.Range("A1", ws.Range("A" & Rows.Count).End(xlUp))
        evl = Evaluate("IF(" & rng.Address(external:=True) & "<>"""", """ & ws.Name & """, """")")
        rng.Offset(, 1).Value = evl
    Next ws

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

AlanConsultantCommented:
Hi Steve,

Do you specifically want this in VBA?

You can easily do it with just a worksheet function:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)

Open in new window


The cell reference can be to any cell in the worksheet.

Just copy down to whatever range you like.

Alan.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this...

Sub SheetName()
Dim ws As Worksheet
Dim lr As Long, i As Long
Application.ScreenUpdating = False
For Each ws In Sheets
    lr = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 2 To lr
        If Application.CountA(ws.Rows(i)) > 0 Then
            ws.Cells(i, 2).Value = ws.Name
        End If
    Next i
Next ws
Application.ScreenUpdating = True
End Sub

Open in new window

0
Steve LowryPrincipal ChemistAuthor Commented:
Norie,
Your code worked perfectly for me.
Thanks.
Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.