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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

NorieAnalyst Assistant Commented:

Is there a particular column the sheet name should go in?
Steve LowryPrincipal ChemistAuthor Commented:
Column B will work.  But any column that you designate, I can work with.
NorieAnalyst Assistant Commented:
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

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Hi Steve,

Do you specifically want this in VBA?

You can easily do it with just a worksheet function:


Open in new window

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

Just copy down to whatever range you like.

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

Steve LowryPrincipal ChemistAuthor Commented:
Your code worked perfectly for me.
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.