VBA and spaces in workbook name

OxfordCurmudgeon

New Member
Joined
Oct 24, 2023
Messages
21
Office Version
  1. 2010
  2. 2003 or older
Platform
  1. Windows
My goal is to copy a named range from one workbook to another. The fun arises because these are two generations of the same workbook. The named ranges and sheet names are the same in both workbooks. I need to copy the contents from the EmployeeInfo range in the older workbook into the same named range in the newer workbook. Should be simple, but I'm getting tripped up because there are spaces in the workbook names. I concocted a much simplified version in hopes of getting some insights here.

Two workbooks: OldBook.xlsm and Old Book.xlsm. The only difference is the space in the name. (I was going to upload the two files, but apparently XL2BB doesn't do workbooks.) If you want to follow along, create a new worksheet with at least two worksheets. On Sheet2, create a named range called EmployeeInfo. (Size and location are not important.) Save it as OldBook.xlsm, then again as Old Book.xlsm.

The code is below. Set up 8 variations on a string to specify a named range. r1, r3, and r5 refer to OldBook; r2, r4, and r6 refer to Old Book. Each string then gets passed to the Range command and the results reported -- either success (the range name) or ethe rror #. r1, r3, and r5 all work; the Range statements return the expected address range. r2 and r4 give error 1004; tripped up by the space in the workbook name. r6 works and gives us a rule that works in all cases: Put the workbook name in square brackets, and append the worksheet name. Put all that in single quiotes followed by an exclamation point and the range name. Finally put all that in double quotes.

But for a workbook-scope named range, we shouldn't need to specify the sheet name. R1 shows that workbook name and range name are sufficient when there are no spaces in the workbook name. R7 and r8 are my attempts to get the same outcome when there are spaces in the workbook name. Neither works.

Question #1: Is there a way to make the r1-style reference work when there are spaces in the workbook name?

Question #2: Am I missing a simpler solution?

RESULTS:

R1: $B$2:$C$3
R2: Err #1004
R3: $B$2:$C$3
R4: Err #1004
R5: $B$2:$C$3
R6: $B$2:$C$3
R7: Err #1004
R8: Err #1004

CODE:

VBA Code:
Private Sub Test()

r1 = "OldBook.xlsm!EmployeeInfo"
r2 = "Old Book.xlsm!EmployeeInfo"
r3 = "[OldBook.xlsm]Sheet2!EmployeeInfo"
r4 = "[Old Book.xlsm]Sheet2!EmployeeInfo"
r5 = "'[OldBook.xlsm]Sheet2'!EmployeeInfo"
r6 = "'[Old Book.xlsm]Sheet2'!EmployeeInfo"
r7 = "'[Old Book.xlsm]'!EmployeeInfo"
r8 = "Old%20Book.xlsm!EmployeeInfo"


On Error Resume Next

Err.Number = 0
Debug.Print "R1: " & Range(r1).Address
If Err.Number <> 0 Then Debug.Print "R1: Err #" & Err.Number

Err.Number = 0
Debug.Print "R2: " & Range(r2).Address
If Err.Number <> 0 Then Debug.Print "R2: Err #" & Err.Number

Err.Number = 0
Debug.Print "R3: " & Range(r3).Address
If Err.Number <> 0 Then Debug.Print "R3: Err #" & Err.Number

Err.Number = 0
Debug.Print "R4: " & Range(r4).Address
If Err.Number <> 0 Then Debug.Print "R4: Err #" & Err.Number
   
Err.Number = 0
Debug.Print "R5: " & Range(r5).Address
If Err.Number <> 0 Then Debug.Print "R5: Err #" & Err.Number

Err.Number = 0
Debug.Print "R6: " & Range(r6).Address
If Err.Number <> 0 Then Debug.Print "R6: Err #" & Err.Number

Err.Number = 0
Debug.Print "R7: " & Range(r7).Address
If Err.Number <> 0 Then Debug.Print "R7: Err #" & Err.Number

Err.Number = 0
Debug.Print "R8: " & Range(r8).Address
If Err.Number <> 0 Then Debug.Print "R8: Err #" & Err.Number

On Error GoTo 0

End Sub
 
Last edited by a moderator:
Try it
Code:
Private Sub test()

r1 = "'OldBook.xlsm'!EmployeeInfo"
r2 = "'Old Book.xlsm'!EmployeeInfo"

On Error Resume Next

Err.Number = 0
Debug.Print "R1: " & Range(r1).Address(external:=True)
If Err.Number <> 0 Then Debug.Print "R1: Err #" & Err.Number

Err.Number = 0
Debug.Print "R2: " & Range(r2).Address(external:=True)
If Err.Number <> 0 Then Debug.Print "R2: Err #" & Err.Number

On Error GoTo 0

End Sub
 
Upvote 0
Solution
@OxfordCurmudgeon
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block at the bottom of this post has more details. I have added the tags for you this time. 😊
 
Upvote 0
Try it
Code:
Private Sub test()

r1 = "'OldBook.xlsm'!EmployeeInfo"
r2 = "'Old Book.xlsm'!EmployeeInfo"

On Error Resume Next

Err.Number = 0
Debug.Print "R1: " & Range(r1).Address(external:=True)
If Err.Number <> 0 Then Debug.Print "R1: Err #" & Err.Number

Err.Number = 0
Debug.Print "R2: " & Range(r2).Address(external:=True)
If Err.Number <> 0 Then Debug.Print "R2: Err #" & Err.Number

On Error GoTo 0

End Sub

While this is technically a new question, it's related so I'm continuing the thread instead of starting a new one.

I modified my dummy spreadsheets. Old.xlsm has three sheets, the second one is called "WhoDoesWhat" and it has a workbook named range of "EmployeeInfo". I also saved it as New.xlsm. In New.xlsm, I created the following code (derived from yours):

VBA Code:
Private Sub test()
Dim SourceRangeName As String
Dim DataHold As Variant
SourceRangeName = _
    "'" & ActiveWorkbook.Name _
    & "'!EmployeeInfo"
Debug.Print "Range name: " & SourceRangeName
Debug.Print "Range: " & Range(SourceRangeName).Address(External:=True)
End Sub

Starting with nothing open in Excel, I open New, then open Old and run the code (from New). It works perfectly. I closed Old and opened a real workbook. (Let's call it RealOld.xlsm.) Again, the code works perfectly. It shows the fully qualified range name and the actual addresses:

Range name: 'RealOld.xlsm'!EmployeeInfo
Range: '[RealOld.xlsm]WhoDoesWhat'!$A$2:$C$201

Now it gets weird. I copied the code into my actual workbook; let's call it RealNew.xlsm. I close everything. Open RealNew and RealOld and single step through the code. First print statement works:

Range name: 'RealOld.xlsm'!EmployeeInfo

But when I step through the next instruction, I get "Run-time error: '1004': Application-defined or object-defined error

I'm baffled. The code should be correct as I copied and pasted. It shouldn't be the actual name of RealOld.xlsm; while it's long and complex -- it works with the test harness. While in the real Sub there is other code that would come before this (to prompt the user for a file name and to then open that file), I commented all of it out.

Any insights?
 
Upvote 0
I don't understand what your problem is. I don't know English anyway. I understand it like this: You opened a new workbook -> you add to have 3 sheets, of which the second sheet is named "WhoDoesWhat" --> you add Name "EmployeeInfo" (A2:C201) -> you saved as Old.xlsm -> you saved as RealOld.xlsm --> you add to RealOld.xlsm code
VBA Code:
Private Sub test()
Dim SourceRangeName As String
Dim DataHold As Variant
    SourceRangeName = "'" & ActiveWorkbook.Name & "'!EmployeeInfo"
    Debug.Print "Range name: " & SourceRangeName
    Debug.Print "Range: " & Range(SourceRangeName).Address(External:=True)
End Sub

-> you saved as New.xlsm -> you will save as RealNew.xlsm.

Old.xlsm and RealOld.xlsm do not have code, New.xlsm and RealNew have code. Is that what happened?

I opened RealNew.xlsm, RealOld.xlsm -> I ran the code in RealNew.xlsm and I saw:
VBA Code:
Range name: 'RealOld.xlsm'!EmployeeInfo 
Range: [RealOld.xlsm]WhoDoesWhat!$A$2:$C$201

-> I selected RealNew.xlsm -> I ran the code in RealNew.xlsm and I saw:
VBA Code:
Range name: 'RealNew.xlsm'!EmployeeInfo 
Range: [RealNew.xlsm]WhoDoesWhat!$A$2:$C$201

And what was your problem?
 
Upvote 0
I don't understand what your problem is. I don't know English anyway. I understand it like this: You opened a new workbook -> you add to have 3 sheets, of which the second sheet is named "WhoDoesWhat" --> you add Name "EmployeeInfo" (A2:C201) -> you saved as Old.xlsm -> you saved as RealOld.xlsm --> you add to RealOld.xlsm code
VBA Code:
Private Sub test()
Dim SourceRangeName As String
Dim DataHold As Variant
    SourceRangeName = "'" & ActiveWorkbook.Name & "'!EmployeeInfo"
    Debug.Print "Range name: " & SourceRangeName
    Debug.Print "Range: " & Range(SourceRangeName).Address(External:=True)
End Sub

-> you saved as New.xlsm -> you will save as RealNew.xlsm.

Old.xlsm and RealOld.xlsm do not have code, New.xlsm and RealNew have code. Is that what happened?

I opened RealNew.xlsm, RealOld.xlsm -> I ran the code in RealNew.xlsm and I saw:
VBA Code:
Range name: 'RealOld.xlsm'!EmployeeInfo
Range: [RealOld.xlsm]WhoDoesWhat!$A$2:$C$201

-> I selected RealNew.xlsm -> I ran the code in RealNew.xlsm and I saw:
VBA Code:
Range name: 'RealNew.xlsm'!EmployeeInfo
Range: [RealNew.xlsm]WhoDoesWhat!$A$2:$C$201

And what was your problem?
Old.xlsm and RealOld.xlsm do have code -- but it's not being executed. The code I was running was in New.xlsm, and RealNew.xlsm.

The problem: When I execute this line of code in RealNew.xlsm, I get an error 1004.

VBA Code:
Debug.Print "Range: " & Range(SourceRangeName).Address(External:=True)
 
Upvote 0
Old.xlsm and RealOld.xlsm do have code -- but it's not being executed. The code I was running was in New.xlsm, and RealNew.xlsm.

The problem: When I execute this line of code in RealNew.xlsm, I get an error 1004.

VBA Code:
Debug.Print "Range: " & Range(SourceRangeName).Address(External:=True)
Otworzyłem RealNew.xlxm z Name EmployeeInfo (A2:C201) i kod -> zapisałem jako RealOld.xlsm -> zamknąłem Excel

Otworzyłem RealNew.xlsm --> otworzyłem RealOld.xlsm -> uruchomiłem kod w RealNew.xlsm i widziałem:
Range name: 'RealOld.xlsm'!EmployeeInfo
Range: [RealOld.xlsm]WhoDoesWhat!$A$2:$C$201


-> wybrałem sheet w RealNew.xlsm (RealNew.xlsm aktywny) -> uruchomiłem kod w RealNew.xlsm i widziałem:
Range name: 'RealNew.xlsm'!EmployeeInfo
Range: [RealNew.xlsm]WhoDoesWhat!$A$2:$C$201


Nie otrzymałem żadnego błędu.
 
Upvote 0
Otworzyłem RealNew.xlxm z Name EmployeeInfo (A2:C201) i kod -> zapisałem jako RealOld.xlsm -> zamknąłem Excel

Otworzyłem RealNew.xlsm --> otworzyłem RealOld.xlsm -> uruchomiłem kod w RealNew.xlsm i widziałem:
Range name: 'RealOld.xlsm'!EmployeeInfo
Range: [RealOld.xlsm]WhoDoesWhat!$A$2:$C$201


-> wybrałem sheet w RealNew.xlsm (RealNew.xlsm aktywny) -> uruchomiłem kod w RealNew.xlsm i widziałem:
Range name: 'RealNew.xlsm'!EmployeeInfo
Range: [RealNew.xlsm]WhoDoesWhat!$A$2:$C$201


Nie otrzymałem żadnego błędu.
Your English is much better than my Polish! And your first answer put me on the right track. I am going to wait to see if others here can offer any insights.
 
Upvote 0

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