ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   macro-import another xls file (https://www.excelbanter.com/excel-discussion-misc-queries/260396-macro-import-another-xls-file.html)

puiuluipui

macro-import another xls file
 
Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!

Chip Pearson

macro-import another xls file
 
Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!


puiuluipui

macro-import another xls file
 
Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!

.


Dave Peterson

macro-import another xls file
 
Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!

.


--

Dave Peterson

puiuluipui

macro-import another xls file
 
Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.


Chip Pearson

macro-import another xls file
 
What do you mean by "import"? The code I posted will display the file
open dialog to prompt you to select the file. Once that is complete,
you need to open the file:

Dim WB As Workbook
Set WB = Application.Workbooks.Open(Fname)

where FName is the file name selected in the Open dialog. Beyond that
you need to explain just you mean by "want to import an xls".

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Thu, 1 Apr 2010 06:28:08 -0700, puiuluipui
wrote:

Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.


Gord Dibben

macro-import another xls file
 
Opening a file does not "import" it into anything.

What portion of that opened file do you need imported to an active workbook?

That's what you need to describe.


Gord Dibben MS Excel MVP

On Thu, 1 Apr 2010 06:28:08 -0700, puiuluipui
wrote:

Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.



puiuluipui

macro-import another xls file
 
Hi, i will tell you what i tried, step by step. I'm not good at explaining,
so if i tell you what steps i've made you will understand better.
ToolsMacroRecord new macroDataImport external dataImport data and from
the browse window i selected an excel file that has been imported. This is
the code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 01.04.2010 by puiuluipui
'

'
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;User
ID=Admin;Data Source=C:\Users\puiuluipui\Desktop\Alex\excel.xls; Mode=Share
Den" _
, _
"y Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Je" _
, _
"t OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=" _
, _
"1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System
Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Cop" _
, _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("Sheet1$")
.Name = "excel"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Users\puiuluipui\Desktop\Alex\excel.xls"
.Refresh BackgroundQuery:=False
End With
End Sub


The problem is that this macro will allways open only the file named "excel".
What i need is that after i run this code, to be able to choose what file i
want.

I guess that some lines from this code must be changed. Perhaps the lines
containing the file source.

Can this code be made to let me choose everytime what file to import?
Basically to do exactly like i did in the first place when i recorded the
macro.

Can this be done?
Thanks!







"Gord Dibben" wrote:

Opening a file does not "import" it into anything.

What portion of that opened file do you need imported to an active workbook?

That's what you need to describe.


Gord Dibben MS Excel MVP

On Thu, 1 Apr 2010 06:28:08 -0700, puiuluipui
wrote:

Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.


.


Dave Peterson

macro-import another xls file
 
Try:

Sub Macro1()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
exit sub 'user hit cancel
end if

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;" _
& "User ID=Admin;Data Source=" & fname & ";Mode=Share Den" _

....rest of your recorded code here.

.....

(Untested, uncompiled. Watch for typos.)

puiuluipui wrote:

Hi, i will tell you what i tried, step by step. I'm not good at explaining,
so if i tell you what steps i've made you will understand better.
ToolsMacroRecord new macroDataImport external dataImport data and from
the browse window i selected an excel file that has been imported. This is
the code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 01.04.2010 by puiuluipui
'

'
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;User
ID=Admin;Data Source=C:\Users\puiuluipui\Desktop\Alex\excel.xls; Mode=Share
Den" _
, _
"y Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Je" _
, _
"t OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=" _
, _
"1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System
Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Cop" _
, _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("Sheet1$")
.Name = "excel"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Users\puiuluipui\Desktop\Alex\excel.xls"
.Refresh BackgroundQuery:=False
End With
End Sub

The problem is that this macro will allways open only the file named "excel".
What i need is that after i run this code, to be able to choose what file i
want.

I guess that some lines from this code must be changed. Perhaps the lines
containing the file source.

Can this code be made to let me choose everytime what file to import?
Basically to do exactly like i did in the first place when i recorded the
macro.

Can this be done?
Thanks!

"Gord Dibben" wrote:

Opening a file does not "import" it into anything.

What portion of that opened file do you need imported to an active workbook?

That's what you need to describe.


Gord Dibben MS Excel MVP

On Thu, 1 Apr 2010 06:28:08 -0700, puiuluipui
wrote:

Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.


.


--

Dave Peterson

puiuluipui

macro-import another xls file
 
It works great!
Thanks!

"Dave Peterson" wrote:

Try:

Sub Macro1()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
exit sub 'user hit cancel
end if

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;" _
& "User ID=Admin;Data Source=" & fname & ";Mode=Share Den" _

....rest of your recorded code here.

.....

(Untested, uncompiled. Watch for typos.)

puiuluipui wrote:

Hi, i will tell you what i tried, step by step. I'm not good at explaining,
so if i tell you what steps i've made you will understand better.
ToolsMacroRecord new macroDataImport external dataImport data and from
the browse window i selected an excel file that has been imported. This is
the code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 01.04.2010 by puiuluipui
'

'
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;User
ID=Admin;Data Source=C:\Users\puiuluipui\Desktop\Alex\excel.xls; Mode=Share
Den" _
, _
"y Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Je" _
, _
"t OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=" _
, _
"1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System
Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Cop" _
, _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("Sheet1$")
.Name = "excel"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Users\puiuluipui\Desktop\Alex\excel.xls"
.Refresh BackgroundQuery:=False
End With
End Sub

The problem is that this macro will allways open only the file named "excel".
What i need is that after i run this code, to be able to choose what file i
want.

I guess that some lines from this code must be changed. Perhaps the lines
containing the file source.

Can this code be made to let me choose everytime what file to import?
Basically to do exactly like i did in the first place when i recorded the
macro.

Can this be done?
Thanks!

"Gord Dibben" wrote:

Opening a file does not "import" it into anything.

What portion of that opened file do you need imported to an active workbook?

That's what you need to describe.


Gord Dibben MS Excel MVP

On Thu, 1 Apr 2010 06:28:08 -0700, puiuluipui
wrote:

Hi, i just want to import an xls file, but thru an macro, and after i import
the file, i will add some codes myself. I need to hit a button linked to an
macro and the macro to open an browse window. I will select the file i need,
hit ok and the excel to import in active sheet, that file. After this, the
macro will format the content, but this is another problem i will deal with
later. I only need the macro to import a file by browsing after it.

Can this be done?
Thanks!

"Dave Peterson" a scris:

Chip's code shows you how to get the name of the file from the user. It doesn't
do the actual import.

You'll have to add that portion yourself -- or explain what you're doing in
greater detail.


puiuluipui wrote:

Hi, i have some problems.
The code run corectly and browse for whatever file i need, but when i click
that file, it doesn;t import anything!
What am i doing wrong? I have excel 2003.
Thanks!

"Chip Pearson" a scris:

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim Filter As String
' Excel 2003 workbooks
Filter = "Excel Files (*.xls),*.xls"
' Excel 2007 workbooks workbooks
Filter = "Excel Files (*.xlsx;*.xlsm;*.xlsb),*.xlsx;*.xlsm;*.xlsb"
' Excel 2003 and 2007
Filter = "Excel Files
(*.xls;*.xlsx;*.xlsm;*.xlsb),*.xls;*.xlsx;*.xlsm;* .xlsb"

FName = Application.GetOpenFilename(filefilter:=Filter)
If FName = False Then
Debug.Print "cancel"
Else
Debug.Print "selected: " & FName
End If
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Wed, 31 Mar 2010 14:00:01 -0700, puiuluipui
wrote:

Hi, i need a macro to import another xls file.When i run macro i need a
window to appear and to browse to the file i need to import. I allways open
another file, so i want to browse for the file.
Can this be done?
Thanks!
.


--

Dave Peterson
.


.


--

Dave Peterson
.



All times are GMT +1. The time now is 02:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com