jmvanhilten
New Member
- Joined
- Feb 25, 2010
- Messages
- 2
Hi,
I have some code to read in several textfiles in specific sheets in a workbook. The problem is weird.
-First, If the name of the textfiles is hardcoded everything works fine.
-Second, if the name of the textfiles is read out fo the excell-sheet, it sometimes works and sometimes it doesn't.
Anyone any idea?
I have some code to read in several textfiles in specific sheets in a workbook. The problem is weird.
-First, If the name of the textfiles is hardcoded everything works fine.
-Second, if the name of the textfiles is read out fo the excell-sheet, it sometimes works and sometimes it doesn't.
Code:
Public Sub FixedWidthImport()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ThePath As String
Dim TheName As String
Dim Stat As String
' read several cells for .txt file location and name
Worksheets("MacroVariables").Activate
ThePath = ActiveSheet.Range("$C$11")
TheName = ActiveSheet.Range("$C$12")
'call sub to read file
Stat = "Static01"
Call Static01(ThePath, TheName, Stat)
End Sub
Public Sub Static01(ThePath, TheName, Stat)
Dim Name1 As String
Dim Name2 As String
Worksheets("Static01").Activate
ActiveSheet.Columns("A:I").Select
Selection.ClearContents
Name1 = ThePath & "\Output\" & TheName & "_" & Stat & ".out"
Name2 = TheName & "_" & Stat & ""
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & Name1, Destination:=ActiveSheet.Range("$A$3"))
.Name = Name2
.PreserveFormatting = True
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePlatform = 437
.TextFileStartRow = 11
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(5, 5, 10, 10, 10, 10, 10, 10)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
End With
Worksheets("MacroVariables").Range("F6:R6").Copy
ActiveSheet.Range("A1:M1").Select
ActiveSheet.Paste
End Sub
Anyone any idea?