samedi 28 février 2015

Text to excel file input prompt

I have a problem with creating a excel. While I created macros before using the record function, it does not work for this problem. Any help would be greatly appreciated!


I have to import a bunch of delimited text files into excel and while the wizard is of great help I'd like to automate the whole process.


What I did was to record the process of importing one file with the wizard. When I rerun the recorded excel I get the following error, though: "Run time error '5': Invalid procedure or call argument"


When I debug it shows an error in the .CommandType = 0 line.


I have two questions, how do I get rid of the error message and more importantly, how can I change the code so that a file input window will pop up every time I run the macro? (I tried to embed the Application.GetOpenFilename() command, to no success, unfortunately)


Thank you already for any help!


Cheers, dahlai


This is my code:



Sub ImportDataPeriod1()
'
' ImportDataPeriod1 Macro
'

'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;F:\pETMEOH100 15xx DHAP.txt", Destination:=Range("$A$3"))
.CommandType = 0
.Name = "pETMEOH100 15xx DHAP"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 37
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 9, 1, 9, _
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 _
, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Aucun commentaire:

Enregistrer un commentaire