Importing Files

Ryujin

New Member
Joined
Apr 17, 2017
Messages
12
Good Morning Friends ,

I need help to get a fuction to import Excel Files with VBA ,
I need to import the File it is entered in a Worksheet with the Name "Info" .

For Help you I will send the Excel that im working and the Excel that I want to import
clear.png


Best Regards and ty
:)
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
See if this does what you want. Please test on copies of your workbooks!
Code:
Option Explicit

Public Sub ImportWorkbook()

  Dim sWorkbookName As String
  Dim wbImport As Workbook
  Dim wsImport As Worksheet
  Dim sMessage As String
  
[COLOR=#008000]  ' ask user for new filename every time this routine runs
[/COLOR]  sWorkbookName = Application.GetOpenFilename(FileFilter:="Excel files (*.xl*), *.xl*", MultiSelect:=False)
[COLOR=#008000]  ' if they press Escape or click Cancel, exit routine now
[/COLOR]  If sWorkbookName = "False" Then Exit Sub
  
[COLOR=#008000]  ' open imported workbook - but disable workbook_open macro from running first!
[/COLOR]  Application.EnableEvents = False
  Set wbImport = Workbooks.Open(sWorkbookName, , True)
  Application.EnableEvents = True
  
[COLOR=#008000]  ' cycle round each worksheet, import it and save its new name
[/COLOR]  For Each wsImport In wbImport.Sheets
    wsImport.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    sMessage = sMessage & vbTab & ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name & vbCrLf
  Next wsImport
  
[COLOR=#008000]  ' close imported workbook
[/COLOR]  wbImport.Close SaveChanges:=False
  
  MsgBox sWorkbookName & " imported.  New worksheets are:-" & vbCrLf & Space(20) & vbCrLf _
         & sMessage & vbCrLf, vbOKOnly + vbInformation

End Sub
 
Upvote 0
The Code that ur giving open the file ... But i need to import the information of the file to the "ProgramaDavid" ...
Did u understand me know?
 
Upvote 0
That code needs to go in the file you want to bring the other file into. So if your main workbook is called ProgramaDavid, you put the code in there. Then when you run the code, it opens a dialog box where you choose the file you want to import into ProgramaDavid.
 
Upvote 0
That code needs to go in the file you want to bring the other file into. So if your main workbook is called ProgramaDavid, you put the code in there. Then when you run the code, it opens a dialog box where you choose the file you want to import into ProgramaDavid.


Im inserting in the button ! But dont Work :/ We open a Dialog box as u say! But when i select the file , The program dont import ... we just open the file that i selected!
 
Upvote 0
Code:
Option ExplicitPrivate Sub Btn_1_Click()
 Dim sWorkbookName As String
  Dim wbImport As Workbook
  Dim wsImport As Worksheet
  Dim sMessage As String
  
  ' ask user for new filename every time this routine runs
  sWorkbookName = Application.GetOpenFilename(FileFilter:="Excel files (*.xl*), *.xl*", MultiSelect:=False)
  ' if they press Escape or click Cancel, exit routine now
  If sWorkbookName = "False" Then Exit Sub
  
  ' open imported workbook - but disable workbook_open macro from running first!
  Application.EnableEvents = False
  Set wbImport = Workbooks.Open(sWorkbookName, , True)
  Application.EnableEvents = True
  
  ' cycle round each worksheet, import it and save its new name
  For Each wsImport In wbImport.Sheets
    wsImport.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    sMessage = sMessage & vbTab & ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name & vbCrLf
  Next wsImport
  
  ' close imported workbook
  wbImport.Close SaveChanges:=False
  
  MsgBox sWorkbookName & " imported.  New worksheets are:-" & vbCrLf & Space(20) & vbCrLf _
         & sMessage & vbCrLf, vbOKOnly + vbInformation


End Sub


Thats The button that im using !

And now its giving an error :
Run Time Error 1004
Method 'Copy' of object '_Worksheet' Failed


Ty for the Effort!!!
 
Upvote 0
What is the name of the workbook you have put the code into?

And what is the name of the workbook you want to import?
 
Upvote 0
wsImport.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) its this the line of the error.


Friend the name of the file that i put the code intro its ProgramaDavid!

And i dont want a expecific name :/ Because some times i would put some WorkBooks With the diferent names to do the same function!
 
Upvote 0
So the code is in ProgramaDavid. When the dialog box opens, you're not selecting ProgramaDavid, are you?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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