Link to home
Start Free TrialLog in
Avatar of hallpett
hallpett

asked on

Use VBA to find the name of the first sheet in a workbook

How can I use vba to find the name of the first sheet in a workbook?
I use this code to copy from the first sheet to another sheet in the workbook:
Sheets("Sheet1").Range("b10:d10").Copy Sheets("Sales").Range("B2")

What I want is to use some vba code to return name of the first sheet in the workbook to a vaiable that I can use in the code. Something like this:
strFirstSheet = "somehow find name of first sheet"
Sheets(strFirstSheet).Range("b10:d10").Copy Sheets("Sales").Range("B2")

Maybe a strange problem but it makes sense in my setting. Appreciate any help.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since VBA starts with 1 instead of 0, the below statement will return the name of the first sheet
Public Function GetFirstSheet() As String
    GetFirstSheet = Application.Sheets.Item(1).Name
End Function

Open in new window