vba to import data

chouston

Board Regular
Joined
Apr 24, 2002
Messages
68
Hi all, I'm trying to automate importing data so I used the macro recorder to code the initial part. Then I added GetOpenFile method so a different file can be selected each time. However, I'm having trouble incorporating this file into the query.

Here's the code:


sub getrentroll()
rentroll = Application.GetOpenFilename(, , "Select a File to Import", , False)


With ActiveSheet.QueryTables.Add(Connection:=rentroll.Text, _
Destination:=Range("A1"))
.Name = "Hacienda"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 10
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 1, 1, 1, 9, 1, 9, 3, 9)
.TextFileFixedColumnWidths = Array(11, 3, 34, 6, 6, 5, 24, 13)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
oops, I think I figured out the filename problem, but when I try to run, I get:

runtime error '1004'

excel cannot find text file to refresh this external data range
 
Upvote 0
p.s. when I choose debug, it highlights the last line: ".Refresh BackgroundQuery:=False"

It also does this when I change it to True
 
Upvote 0
Hi! I'm still stuck with this problem...

I'm trying to automate the import external database, I'd like my coworkers to be able to run this macro and then just pick the file. However when I try to run this, the getopenfilename works, but it won't import, debugging gives me the message I typed in the previous reply. Here's what I have so far:

Sub getrentroll()

rentroll = Application.GetOpenFilename(, , "Select a File to Import", , False)

With ActiveSheet.QueryTables.Add(Connection:="TEXT;rentroll", _
Destination:=Range("A1"))
.Name = "Hacienda"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 10
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 1, 1, 1, 9, 1, 9, 3, 9)
.TextFileFixedColumnWidths = Array(11, 3, 34, 6, 6, 5, 24, 13)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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