Workbook names stored as variables cause "Subscript out of range" error when copying/pasting.

Kandel

New Member
Joined
Jul 26, 2012
Messages
1
I'm trying to use this macro to copy data from one unknown workbook and paste it into another (also unknown). It should pop up two userforms, the first asking which file to use as the source and the second asking which file to use as the paste target. I trimmed off the .xls file type (although I have tried it with and without this part) by just removing the last 4 characters. This all works correctly and the MsgBox lines pop up with the correct names of the files.

When the code gets to the command to copy and paste from workbook to workbook I get a run-time error 9: subscript out of range. I believe that means that the code cannot find the workbooks and the name is wrong. If I type in the name of the workbooks (exactly as seen in the msgbox pop ups) then the copy/paste runs fine, but with the variable in the Workbooks("variable") portion it continues to give me that error.

Does anyone have any idea why this would be happening or what a solution might be?

Thanks for your help.




Module 1

Option Explicit

Public Source As String
Public TargetFile As String
Public Stopped As Boolean


Sub test()

Stopped = False

UserForm1.Show

If Stopped Then Exit Sub

UserForm2.Show

If Stopped Then Exit Sub

Source = Left(Source, Len(Source) - 4)
TargetFile = Left(TargetFile, Len(TargetFile) - 4)

MsgBox Source
MsgBox TargetFile

Application.DisplayAlerts = False

Workbooks("Source").Sheets("Historical Analysis").Range("B4:C4").Copy Destination:=Workbooks(TargetFile).Sheets("Historical Analysis").Range("B4:C4")

Application.DisplayAlerts = True

End Sub



UserForm1


Option Explicit

Private Sub CommandButton1_Click()
Source = Me.ComboBox1.Value
Unload Me
End Sub

Private Sub CommandButton2_Click()
Stopped = True
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim wkb As Workbook
Me.Label1.Caption = "Please select the source file."
With Me.ComboBox1
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub



UserForm2


Option Explicit

Private Sub CommandButton1_Click()
TargetFile = Me.ComboBox1.Value
Unload Me
End Sub

Private Sub CommandButton2_Click()
Stopped = True
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim wkb As Workbook
Me.Label1.Caption = "Please select one of the following files..."
With Me.ComboBox1
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub



 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Workbooks("Source").Sheets("Historical Analysis").Range("B4:C4").Copy Destination:=Workbooks(TargetFile).Sheets("Historical Analysis").Range("B4:C4")
Do you have a workbook named Source or should it be
Code:
Workbooks(Source).Sheets("Historical Analysis").Range("B4:C4").Copy Destination:=Workbooks(TargetFile).Sheets("Historical Analysis").Range("B4:C4")
??

Bye
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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