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:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Re: VBA import file macro, really cool

Try...

Code:
Dim sFilename As Variant
Dim wsDest As Worksheet

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

Hope this helps!
 
Upvote 0
Re: VBA import file macro, really cool

Hey Domenic!!

Yeah, that did it all right! Worked like a charm!


Thanks Again,
Pin
 
Upvote 0
Re: VBA import file macro, really cool

Hey,

Is there anyway for an excel file import via the web, if the web has the excel file represented as an icon without the user first downloading its file first prior to the import taking place??

Thanks
 
Upvote 0
Re: VBA import file macro, really cool

Hi Everybody!

Does anyone know where I can put a specific title for the pop-up open file icon/box in the code below??



Code:
Dim sFilename As Variant
Dim wsDest As Worksheet

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

For example, if I wanted to put
Select File
as the title, where would that go in the code??

Thanks,
Pinaceous
 
Upvote 0
Re: VBA import file macro, really cool

This might help you to know the parameters

GetOpenFilename( FileFilter , FilterIndex , Title , ButtonText , MultiSelect )
 
Last edited:
Upvote 0
Re: VBA import file macro, really cool

Hi,

Can someone help me combine what I have in my code line 3:
Code:
sFilename = Application.GetOpenFilename("Excel Files, *.xls*")
with
GetOpenFilename( FileFilter , FilterIndex , Title , ButtonText , MultiSelect )
that Nine Zero posted.

I can't seem to edit/combine these two parameters in avoiding a VB error.


Thank you!

As I'm really stumped!

-Pinaceous
 
Upvote 0
Re: VBA import file macro, really cool

Hi Everyone,


I've got the title part I'm all good now, I understand.


But now, how do I specify a files path with this application????


For example; c: temp files /folder y /folder x

Please let me know,

Thank you!
Pinaceous
 
Upvote 0
Re: VBA import file macro, really cool

If you want to specify a files path you don't need GetOpenFilename.
use
Code:
sFilename = "c:\temp files \folder y \folder x"
 
Upvote 0
Re: VBA import file macro, really cool

But I want them to select a specific file one at that designed/specified path.

Does this make sense??
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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