I am writing a macro to import .txt files into excel. The files I receive to import are sometimes formatted different so some need to be imported using the import wizard using "Fixed Width" and the other using "Delimiter". I have the correct macro for each, but I want to write an if statement in my macro that tells which macro to run based on the "data type" (the text import wizard is able to see what Data Type the file is; that is delimited or fixed width).
The purpose of the statement below is to "capture" the DataType of the file to be imported, but it is not working correctly. because the code never runs the "Else" statement.
The purpose of the statement below is to "capture" the DataType of the file to be imported, but it is not working correctly. because the code never runs the "Else" statement.
Code:
If DataType = x1FixedWidth Then
Code:
Function open_txt_files(FilePath As String)
'opens txt files and delimits them
Workbooks.OpenText FilePath
If DataType = x1FixedWidth Then
Workbooks.OpenText filename:= _
FilePath, Origin:=437, _
StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(6, _
1), Array(48, 1), Array(105, 1), Array(115, 1), Array(124, 1), Array(133, 1), Array(141, 1) _
, Array(149, 1), Array(160, 1)), TrailingMinusNumbers:=True
Else
Workbooks.OpenText filename:= _
FilePath, Origin:=437, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 1)), TrailingMinusNumbers:=True
End If
End Function