I am trying to do a copy of data between different workbooks. The name for the workbooks happened to be random since users are given the option to select the file names, so I did a set of workbook in my codes. When I included a subroutine into my program, it stated subscript out of range.
Any ideas on how to ensure that the subroutine continue to understand my definition of the workbooks?
My codes are simplified in case you don't quite understand the rationale behind the first sub:
Option Explicit
Sub testwe()
Dim wb As Workbook, wb2 As Workbook, vFile As Variant
Set wb = ActiveWorkbook
vFile = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
Set wb2 = ActiveWorkbook 'set the opened file as wb2
Call test123
End Sub
sub test123()
wb.Worksheets("Sheet1").Range("A1") = wb2.Worksheets("Sheet1").Range("B1")
End Sub
wb
andwb2
for test123. If I run you code withoutOption Explicit
set I get anObject Required
error. Also, are you surewb2
will have a worksheet namedSheet1
? – Doug Glancytest123
thesubscript out of range
error would mean there's no "Sheet1" - spelled exactly like that. I can't see another reason for it, at least. – Doug Glancy