retrieve the names of currently worksheets and enter these names into column A of worksheet, named 'Results'

Dear Experts:

I would like to run a macro that performs the following actions:

The macro is to retrieve the worksheet names of the currently selected worksheets and enter them into Column A (starting from A3) of  the worksheet named 'Results'

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?

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

x
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.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Not quite sure I understand your requirement.

BTW give this a try and see if this is what you are trying to achieve.
Sub GetWorkSheetNames()
Dim ws As Worksheet
Dim r As Long
Sheet1.Range("A3").Value = "Results"
r = 3
For Each ws In ActiveWorkbook.Worksheets
    r = r + 1
    Sheet1.Range("A" & r).Value = ws.Name
Next ws
End Sub

Open in new window

0
Saqib Husain, SyedEngineerCommented:
Here is mine
Sub a()
    Set wsl = ThisWorkbook.Windows(1).SelectedSheets
    For i = 1 To wsl.Count
        Sheets("Results").Cells(2 + i, 1).Value = wsl(i).Name
    Next i
End Sub

Open in new window

0
Saqib Husain, SyedEngineerCommented:
In a more formal way
Sub ListSelectedSheets()
Dim wsl As Sheets
Dim i As Integer
    Set wsl = ThisWorkbook.Windows(1).SelectedSheets
    For i = 1 To wsl.Count
        Sheets("Results").Cells(2 + i, 1).Value = wsl(i).Name
    Next i
End Sub

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
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Now I get what you meant....

Here is the tweaked code
Sub GetWorkSheetNames()
Dim ws As Worksheet, dws As Worksheet
Dim r As Long
Set dws = Sheets("Results")
r = 3
For Each ws In ActiveWindow.SelectedSheets
    If ws.Name <> dws.Name Then
        dws.Range("A" & r).Value = ws.Name
        r = r + 1
    End If
Next ws
End Sub

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Hi Neeraj, thank you very much, you were close, I was asking for selected sheets only. Thank you very much anyway
0
Andreas HermleTeam leaderAuthor Commented:
I was hoping to award points to the two of you, since both codes work fine and I due to the different approaches, I can learn from both. But awarding points for assisted solutions is regrettably not available anymore.

Since Saqib was quicker to answer, I clicked his answer ' best solution '.

thank you very much to you both for your great support.

Regards, Andreas
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Andreas! Glad your issue is resolved.

Actually I couldn't get your requirement correctly first time.
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.