Here is the code. It errors out at "Set SourceRange1".
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName
As String) As Long
*******************************************
Public Sub ChDirNet(szPath As String)
' For Sub GetData & OTHER SUBS IN FORM
' Rob Bovey
Dim lReturn As Long
lReturn = SetCurrentDirectoryA(szPath)
If lReturn = 0 Then Err.Raise vbObjectError + 1, "Error setting path."
End Sub
*******************************************
Function LastRow(sh As Worksheet)
' For Sub GetData
' Find the last real row
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
*******************************************
Function LastCol(sh As Worksheet)
' For Sub GetData
' Find the last real column
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
**********************************************
Sub m02_GetData()
' This Sub uses 4 functions:
' 1. Private Declare Function SetCurrentDirectoryA (at top of module)
' 2. Public Sub ChDirNet(szPath As String)
' 3. Function LastRow(sh As Worksheet)
' 4. Function LastCol(sh As Worksheet)
' Opens each Order Status Spreadsheet in succession and copies to blank
template
'
MsgBox "Please select all files at once" & vbNewLine & vbNewLine & vbNewLine
On Error GoTo ErrorHandler
Dim SaveDriveDir As String
Dim MyPath As String 'Dim FilesInPath As String
Dim MyFiles() As Variant
Dim SourceRcount1, SourceRcount2 As Long
Dim Fnum As Long
Dim basebook, mybook As Workbook
Dim sourceRange1, sourceRange2 As Range
Dim destrange1, destrange2 As Range
Dim rnum1, rnum2 As Long
Dim lrow1, lrow2 As Long
Dim lcol1, lcol2 As Long
SaveDriveDir = CurDir
'Fill in the path\folder where the files are
'on your machine : MyPath = "C:\Data" or on a network :
ChDirNet "\\Sling\taiwan\Order_Status"
MyFiles = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),
*.xls", MultiSelect:=True)
MsgBox "Processing will take about five minutes. That'll be at about " &
Format(DateAdd("n", 5, Now), "medium time") & ", ok? " & vbNewLine &
vbNewLine _
& "Be sure to click OK before you go!"
If IsArray(MyFiles) Then
Application.ScreenUpdating = False
Set basebook = ActiveWorkbook
rnum1 = 1
rnum2 = 1
On Error GoTo ErrorHandler 'CleanUp
'Loop through all files in the array(myFiles)
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Workbooks.Open(MyFiles(Fnum))
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
lrow1 = LastRow(mybook.Sheets(1))
lrow2 = LastRow(mybook.Sheets(2))
lcol1 = LastCol(mybook.Sheets(1))
lcol2 = LastCol(mybook.Sheets(2))
Set sourceRange1 = mybook.Worksheets(1).Range(Cells(1, 1),
Cells(lrow1, lcol1))
'Set sourceRange1 = mybook.Worksheets(1).Range("A1:AA" & lrow1)
Set sourceRange2 = mybook.Worksheets(2).Range(Cells(1, 1),
Cells(lrow2, lcol2))
'Set sourceRange2 = mybook.Worksheets(2).Range("A1:AA" & lrow2)
SourceRcount1 = sourceRange1.Rows.Count
SourceRcount2 = sourceRange2.Rows.Count
Set destrange1 = basebook.Worksheets(1).Range("A" & rnum1)
Set destrange2 = basebook.Worksheets(2).Range("A" & rnum2)
sourceRange1.Copy destrange1
sourceRange2.Copy destrange2
rnum1 = rnum1 + SourceRcount1
rnum2 = rnum2 + SourceRcount2
mybook.Close savechanges:=False
Next Fnum
Else: Exit Sub
Exit Sub
End If
CleanUp:
Application.ScreenUpdating = True
ChDirNet SaveDriveDir
ErrorHandlerNext:
Exit Sub
ErrorHandler:
Err.Raise 1001
'MsgBox "Error " & Err.Number & "; " & Err.Description
'Resume ErrorHandlerNext
End Sub