Error when cancelling a pop up box

LukeDF

New Member
Joined
Mar 17, 2011
Messages
9
Office Version
  1. 365
  2. 2019
I have the attached code to select a set of files to copy data from into the tabs of my workbook. However, I get a type mismatch error 15 if I change my mind and cancel the file selection. Unsuccessfuly tried to add code to check this. Appreciate any help

Sub CopyPasteData()

Dim i As Long
Dim arraySheets As Variant
Dim myFile As Variant
Dim sourceWbk As Workbook
Dim destWbk As Workbook

Application.ScreenUpdating = False

continue:
arraySheets = Array("Notifications", "Orders", "Operations", "Schedule")
Set destWbk = ThisWorkbook
myFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Select a File", MultiSelect:=True)

For i = LBound(arraySheets) To UBound(arraySheets)
If UBound(myFile) = 4 Then
If Sheets(arraySheets(i)).Range("A1") <> "" Then Sheets(arraySheets(i)).Cells.ClearContents
If Sheets(arraySheets(i)).Range("A1") = "" Then
Set sourceWbk = Workbooks.Open(myFile(i + 1))
ActiveSheet.Cells.Copy
destWbk.Sheets(arraySheets(i)).Range("A1").PasteSpecial
Application.CutCopyMode = False
sourceWbk.Close True
Set sourceWbk = Nothing
End If
Else
MsgBox "Please select all 4 data files at once!", vbInformation, "Select Multi Files"
GoTo continue
End If
Next i

MsgBox "All data has been copied!", vbInformation, "Action Complete"

Set destWbk = Nothing
Sheet4.Activate

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi
GetOpenFileName method returns False when Cancel button is pressed & your code needs to handle this.

Try adding the line of code where shown & see if resolves your issue

Rich (BB code):
    myFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Select a File", MultiSelect:=True)
    If myFile = False Then Exit Sub
'rest of code

Dave
 
Upvote 0
whoops line should have read

Rich (BB code):
If VarType(myFile) = vbBoolean Then Exit Sub

sorry about that

Dave
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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