Referencing code name of second workbook vba

jon1315

Board Regular
Joined
Jun 16, 2020
Messages
60
Office Version
  1. 365
Platform
  1. Windows
Morning All
I have two workbooks. The active workbook does some calculations/ has some user input, and then a macro is used to move certain entries to a second workbook.

Both workbooks are set up using code names as somebody will inevitably change the worksheet name at some point (!) but when I try and reference the code name of the second workbook I get a "Compile error: Argument not optional"

I've attached my code below - it's the last two rows that seem to be the problem. If I take my option explicit off then the code will open the WBSuspense, but not be able to find "Log" (which is the correct codename)

just wondering if anyone has any idea what I've done wrong?

Thanks in advance!
Jon


VBA Code:
Sub Move_To_Suspense()
Dim LRow, LRowSuspense As Long
Dim WBMaster, WBSuspense As Workbook
Dim SuspenseFP As String
Set WBMaster = ThisWorkbook
SuspenseFP = Range("suspensefp")
    HHC.Activate
LRow = Cells(Rows.Count, 1).End(xlUp).Row
    Range("a2:i" & LRow).AutoFilter field:=9, Criteria1:="Suspense"
        If Range("a2:a" & LRow).SpecialCells(xlCellTypeVisible).Count = 1 Then
                HHC.ShowAllData
                Instructions.Select
                MsgBox "No rows to move to suspense"
        Exit Sub
        End If
Set WBSuspense = Workbooks.Open(Filename:=SuspenseFP)
    Log.Activate
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You can only use the codename to refer directly to a sheet that's in the same workbook as the code.
 
Upvote 0
You can only directly refer to codenames like that, for the workbook containing the code.
One workround is to loop through the sheets in the other workbook looking at their code name & then set that as a variable.
VBA Code:
Set WBSuspense = Workbooks.Open(Filename:=SuspenseFP)
For Each ws In WBSuspense.Worksheets
   If ws.CodeName = "Log" Then Exit Sub
Next ws
ws.Activate
 
Upvote 0
Solution
You can refer to code names in other workbooks but only if you have a reference set to the other workbook's project (which requires having renamed it to something other than the default VBAProject).
 
Upvote 0
You can only directly refer to codenames like that, for the workbook containing the code.
One workround is to loop through the sheets in the other workbook looking at their code name & then set that as a variable.
VBA Code:
Set WBSuspense = Workbooks.Open(Filename:=SuspenseFP)
For Each ws In WBSuspense.Worksheets
   If ws.CodeName = "Log" Then Exit Sub
Next ws
ws.Activate

Perfect - just changed the exit sub to exit for and it seems to work fine!
 
Upvote 0
You can refer to code names in other workbooks but only if you have a reference set to the other workbook's project (which requires having renamed it to something other than the default VBAProject).

Looks like something for me to look into - every time I'm on here I realise just how little I know!

Thanks all!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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