Vba PERSONAL error because Sheets Code Name

Dao Ha Quang

New Member
Joined
Apr 30, 2023
Messages
20
Office Version
  1. 2016
I have a personal VBA to run many excel files exported by the estimating software.
When I run it, I get error 424 because the sheet name I am using is code name.
Because the sheet name in the excel file is exported from the software, there is special characters so I can't use names.
Is there any way to fix this situation? Thank
1695629867501.png
 

Attachments

  • 1695629798617.png
    1695629798617.png
    66.4 KB · Views: 10

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You will need to fully qualify the sheets with their parent workbook.

Add a function to get the worksheet from its codename like this:
VBA Code:
Function GetSheet(ByVal TargetWorkbookName As String, ByVal CodeName As String) As Worksheet
    Dim sh As Worksheet
    For Each sh In Workbooks(TargetWorkbookName).Sheets
        If sh.CodeName = CodeName Then
            Set GetSheet = sh
            Exit Function
        End If
    Next
End Function

Then you use the function like this :

VBA Code:
GetSheet("Enter_The_Workbook_Name_Here", "Sheet1").Visible = False
GetSheet("Enter_The_Workbook_Name_Here", "Sheet2").Visible = False
GetSheet("Enter_The_Workbook_Name_Here", "Sheet3").Visible = False

' and so on

Bear in mind that if the workbook name contains special characters of its own, you will end up with the same problem but hopefully, this could probably be worked around if the workbook name has a specific pattern or if it is the active workbook.
 
Upvote 1
Assumes you are working on the Active Workbook

VBA Code:
Sub getcodename()

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Select Case ws.CodeName
            Case "Sheet1":  ws.Visible = False
            Case "Sheet3":  ws.Visible = False
        End Select
        
    Next ws

End Sub
 
Upvote 0
Are you sure the error is caused by the special/unicode characters?
When I tried it by naming a sheet with a unicode char (e.g Əsa), I was still able to access the sheet using its code name.
@Jaafar Tribak & @Alex Blakenburg, can you try it?

try 1.jpg


try 2.jpg
 
Upvote 0
Are you sure the error is caused by the special/unicode characters?
When I tried it by naming a sheet with a unicode char (e.g Əsa), I was still able to access the sheet using its code name.
@Jaafar Tribak & @Alex Blakenburg, can you try it?
The issue is not limited to using the Personal workbook.
The issue is that you can't use the codename to refer to a worksheet in a workbook that isn't the workbook containing the code (ThisWorkbook).
 
Upvote 0
it works fine in normal excel file, But I am talking about VBA PERSONAL
The issue is not limited to using the Personal workbook.
The issue is that you can't use the codename to refer to a worksheet in a workbook that isn't the workbook containing the code (ThisWorkbook).

Sorry, I misunderstood the issue.
Do the solutions offered by Jaafar Tribak & Alex Blakenburg work for you?
 
Upvote 0
The issue is that you can't use the codename to refer to a worksheet in a workbook that isn't the workbook containing the code (ThisWorkbook).
You can, but you have to set a reference, which would kind of defeat the purpose here, I suspect. ;)
 
Upvote 0
Sorry, I misunderstood the issue.
Do the solutions offered by Jaafar Tribak & Alex Blakenburg work for you?
It's not very good for my problem, I'll probably still use the simple method of copying VBA to excel files and running it.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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