Worksheet Name vs. Worksheet Codename

kapvg

New Member
Joined
Jun 30, 2018
Messages
25
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi,

I am facing a problem and am unable to figure out the reason after loads of searching...

I have an ActiveX CommandButton on Sheet2 of by workbook that I want to modify (i.e. change font etc.) via VBA code. Sheet2 is named "All Transactions" and I want to make my code work without any modifications even if the sheet is renamed to something else.

The below code works:
Code:
With ThisWorkbook.Sheets("All Transactions").CommandButton1
    .ForeColor = colorBlue
    .BackColor = colorYellow
    .Font.Name = "Calibri"
    .Font.Size = 12
    .Caption = "Click to update data in" & vbNewLine & "Column K"
End With

...but the below code does not :(
Code:
Dim sht2 As Worksheet
Set sht2 = ThisWorkbook.Sheets(2)
With sht2.CommandButton1
    .ForeColor = colorBlue
    .BackColor = colorYellow
    .Font.Name = "Calibri"
    .Font.Size = 12
    .Caption = "Click to update data in" & vbNewLine & "Column K"
End With

Any suggestions on why the 2nd piece of code does not work or what I can do to fix my problem?

thanks,
~kg
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If you're trying to use the codename it should be like
Code:
With Sheet2.CommandButton1
 
Upvote 0
If you're trying to use the codename it should be like
Code:
With Sheet2.CommandButton1

Thanks a ton!!! this works.

Amazingly, I am able to use the variable 'sht2' for all other calculations etc. in my code (in place of ThisWorkbook.Sheets(2)) but only with the CommandButton it does not seem to work.
 
Upvote 0
This
Code:
[COLOR=#333333]ThisWorkbook.Sheets(2)[/COLOR]
is not referring to the codename, it's referring to the sheet index (in this case the 2nd sheet from the left) & is IMO an even worse way of referring to a sheet than the sheet name.
 
Upvote 0
This
Code:
[COLOR=#333333]ThisWorkbook.Sheets(2)[/COLOR]
is not referring to the codename, it's referring to the sheet index (in this case the 2nd sheet from the left) & is IMO an even worse way of referring to a sheet than the sheet name.
Ah...could you please advise on how can I refer to a particular sheet in my code without worrying about changes in the name or location of the sheet?
 
Last edited:
Upvote 0
Thanks 'Fluff' and 'MARK858'.

I have modified my code as below to assign the correct worksheets to variables (sht2 and sht3) based on the Worksheet codenames as below:
Code:
Dim temp2 As Integer
For temp2 = 1 To ThisWorkbook.Sheets.Count Step 1[INDENT]If ThisWorkbook.Sheets(temp2).CodeName = "Sheet2" Then
[/INDENT]
[INDENT=2]Set sht2 = ThisWorkbook.Sheets(temp2)[/INDENT]
[INDENT]End If
If ThisWorkbook.Sheets(temp2).CodeName = "Sheet3" Then
[/INDENT]
[INDENT=2]Set sht3 = ThisWorkbook.Sheets(temp2)[/INDENT]
[INDENT]End If[/INDENT]
Next temp2

but now I am back facing my original problem of trying to access the CommandButton properties on Sheet2 without having to actually use Sheet2.CommandButton1. Is there any way out?

thanks,
~kg
 
Last edited:
Upvote 0
What exactly is your issue with using the sheets codename i.e. Sheet2.CommandButton1 ?
 
Upvote 0
Not sure why you feel the need to assign variables.
Its easier to just use something along the lines of
Code:
Sheet3.CommandButton1
Sheet3.Range("A1").CurrentRegion
 
Upvote 0
My bad.
All the time while wanting to use the CodeName I forgot that Sheet2 is actually the CodeName for that very worksheet !!!! ...duh:LOL:
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top