LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default import external data via macro/vba question

I'm trying to modify a macro to be more robust. What I
want is, with a target file open, to run a macro that will
open a source file and then perform a data import. I have
my modified code below, followed by the original
unmodified with clause.

My suspiscion is the problem lies in the .CommandText =
Array() line. The other possibility might be that the
focus is lost when I open the new file.

So 2 questions.

1) How should the .CommandText = Array() phrase look when
using a dynamic filename?

2) In general when dealing with multiple files in a macro,
how do you keep track and switch focus between them?

My experience with using data-import external data-
import data

is that it only works with the source file already open,
which is why I've added the open file routine at
the beginning of the macro.

Thanks for any help in advance.
Drabbacs

My experience with using data-import external data-
import data

is that it only works with the source file already open.

Code Follows


***Modified With Clause

Sub RTPdataimport()
Dim Filename1 As Variant

Filename1 = Application.GetOpenFilename
(filefilter:="Excel Files, *.xls", _
Title:="Pick a File")

If Filename1 = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=Filename1 '....rest of
recorded code here!


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



***Original With Clause

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" "
"";User ID=Admin;Data Source=C:\Documents and
Settings\Administrator\My Documents\01re" _
, _
"qDB\RTP\RTP desktop lookahead sheets 2004\14 WEEK
LOOK AHEAD042004.xls;Mode=Share Deny Write;Extended
Properties=""HDR=YES;"";Jet " _
, _
"OLEDB:System database="""";Jet OLEDB:Registry
Path="""";Jet OLEDB:Database Password="""";Jet
OLEDB:Engine Type=35;Jet OLEDB:Database L" _
, _
"ocking Mode=0;Jet OLEDB:Global Partial Bulk
Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New
Database Password="""";Jet OL" _
, _
"EDB:Create System Database=False;Jet
OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale
on Compact=False;Jet OLEDB:Compact" _
, " Without Replica Repair=False;Jet
OLEDB:SFP=False"), Destination:=Range( _
"A1"))
.CommandType = xlCmdTable
.CommandText = Array("'14 WEEK LOOK AHEAD042004$'")
.Name = "14 WEEK LOOK AHEAD042004"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = _
"C:\Documents and Settings\Administrator\My
Documents\01reqDB\RTP\RTP desktop lookahead sheets 2004\14
WEEK LOOK AHEAD042004.xls"
.Refresh BackgroundQuery:=False
End With
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import External Data Bongard Excel Discussion (Misc queries) 3 January 8th 10 06:59 PM
External data import B Cut Excel Discussion (Misc queries) 0 October 16th 09 07:11 PM
import external data Swan0tree Excel Discussion (Misc queries) 2 October 1st 09 10:19 PM
Import External Data RFJ Excel Discussion (Misc queries) 1 August 28th 06 12:16 AM
Import External Data - XML mklapp Excel Discussion (Misc queries) 0 October 26th 05 07:04 PM


All times are GMT +1. The time now is 09:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"