VBA import file macro

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with a great macro that allows the user to select a specific excel file in importing it into their already open excel workbook.

Code:
Dim wsDest As Worksheet
Set wsDest = ActiveWorkbook.Sheets(11)
Application.ScreenUpdating = False
On Error Resume Next
With Workbooks.Open(Application.GetOpenFilename("Excel Files, *.xls*"))
.Sheets(10).Range("A1:Q178").Copy wsDest.Range("A1:Q178")
.Close False
End With
On Error GoTo 0
Application.ScreenUpdating = True
Set wsDest = Nothing

Have at it and run the code.


It pops up your typical file to open box.


Now my question is; if the user clicks the 'Cancel' button I'd like a for a message box to pop up and say:

Code:
MsgBox "Not IMPORTING File!!"

and then to
Code:
Exit Sub


Can someone help me piece this together??

Thank you!
Pinaceous
 
Last edited:
Re: VBA import file macro, really cool

Try
Code:
    Dim sFilename As Variant
    Dim wsDest As Worksheet

    ChDrive "C:"
    ChDir "c:\temp files\folder y\folder x"
    
    sFilename = Application.GetOpenFilename("Excel Files, *.xls*")
    If sFilename = False Then
        MsgBox "Not IMPORTING File!!"
        Exit Sub
    End If
    
    Set wsDest = ActiveWorkbook.Sheets(11)
    
Application.ScreenUpdating = False
    On Error Resume Next
    With Workbooks.Open(sFilename)
        .Sheets(10).Range("A1:Q178").Copy wsDest.Range("A1:Q178")
        .Close False
    End With
    On Error GoTo 0
Application.ScreenUpdating = True

Set wsDest = Nothing
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Re: VBA import file macro, really cool

Yes, that's what I want. I was reading about the ChDrive application.

Thank you for bringing it together for me!!! :)
 
Last edited:
Upvote 0
Re: VBA import file macro, really cool

Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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