Reputation: 21
anyone knows how to get the sheet name from the VBA code on that sheet? i.e. : the code on sheet12 will return name of Sheet12, same code on sheet11 will return name of Sheet11? Thanks.
Upvotes: 2
Views: 1913
Reputation: 55
An answer to the issue (= getting the name of the sheet where the code is written, not the name of the active sheet) is Me.Name. If you want the sheet number (=the codename), just use Me.CodeName.
Upvotes: 1
Reputation: 17495
There are two properties you can refer to:
.Name
: This is the name of the worksheet that you can see in the Excel UI.CodeName
: This is the name of the worksheet that you see in the VB editorExample:
MsgBox "Name of the current sheet in Excel: " & ActiveSheet.Name & vbCrLf & _
"Name of the sheet in VB editor: " & ActiveSheet.CodeName
Upvotes: 3
Reputation: 96791
This will give the name of whichever sheet is active:
Sub dural()
MsgBox ActiveSheet.Name
End Sub
Upvotes: 2