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
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