application.getopenfilename if cancel is clicked?

dfida

New Member
Joined
Jan 26, 2010
Messages
49
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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Use: Dim varDave As Variant
 
Upvote 0
Your code tweaked a bit:
Rich (BB code):

Sub testOpenFile()

  Dim varDave
  
  varDave = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
  
  If varDave = False Then
    MsgBox "No file selected. Cannot continue."
    Exit Sub
  End If

  On Error GoTo exit_
  Application.ScreenUpdating = False
  
  With Workbooks.Open(varDave)
    .Sheets("sheet1").Range("a1:a20").Copy
    ThisWorkbook.Sheets("sheet1").Range("a1").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    .Close False
  End With
  ThisWorkbook.Activate
  
exit_:
  
  Application.ScreenUpdating = True
  If Err Then MsgBox Err.Description, vbCritical, "Error"

End Sub
 
Upvote 0
Very nice...thanks again! I have a lot to learn..

Your code tweaked a bit:
Rich (BB code):

Sub testOpenFile()

  Dim varDave
  
  varDave = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
  
  If varDave = False Then
    MsgBox "No file selected. Cannot continue."
    Exit Sub
  End If

  On Error GoTo exit_
  Application.ScreenUpdating = False
  
  With Workbooks.Open(varDave)
    .Sheets("sheet1").Range("a1:a20").Copy
    ThisWorkbook.Sheets("sheet1").Range("a1").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    .Close False
  End With
  ThisWorkbook.Activate
  
exit_:
  
  Application.ScreenUpdating = True
  If Err Then MsgBox Err.Description, vbCritical, "Error"

End Sub
 
Upvote 0
Glad to help, dfida! Thank you for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,679
Messages
6,126,180
Members
449,296
Latest member
tinneytwin

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