I've seen several threads on this, none of which worked for me...mines seems simple?
I want to code below to prompt the user for a file, using application.getopenfilename. When the file is open, it copies a range, then pastes it to ThisWorkBook.
If the user clicks "cancel" I want to macro to end.
So....the "cancel" part works fine (i.e. goes to Exit Sub), but when I actually click on a file, I get a error 13, type mismatch, and the line " If varDave = False Then" is highlighted when I debug.
Any ideas? I'm sure I'm missing something very simple...
Sub testOpenFile()
Dim varDave As String
varDave = Application.GetOpenFilename("Excel Files (*.xlsx), *.xls)")
If varDave = False Then
MsgBox "No file selected. Cannot continue."
Exit Sub
Else
Workbooks.Open varDave
varDave = ActiveWorkbook.Name
Sheets("sheet1").Range("a1:a20").Copy
ThisWorkbook.Activate
Sheets("sheet1").Select
Range("a1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Windows(varDave).Activate
Windows(varDave).Close
End If
End Sub
I want to code below to prompt the user for a file, using application.getopenfilename. When the file is open, it copies a range, then pastes it to ThisWorkBook.
If the user clicks "cancel" I want to macro to end.
So....the "cancel" part works fine (i.e. goes to Exit Sub), but when I actually click on a file, I get a error 13, type mismatch, and the line " If varDave = False Then" is highlighted when I debug.
Any ideas? I'm sure I'm missing something very simple...
Sub testOpenFile()
Dim varDave As String
varDave = Application.GetOpenFilename("Excel Files (*.xlsx), *.xls)")
If varDave = False Then
MsgBox "No file selected. Cannot continue."
Exit Sub
Else
Workbooks.Open varDave
varDave = ActiveWorkbook.Name
Sheets("sheet1").Range("a1:a20").Copy
ThisWorkbook.Activate
Sheets("sheet1").Select
Range("a1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Windows(varDave).Activate
Windows(varDave).Close
End If
End Sub