I need to pick up a text file and get the contents of that file into an Excel Table
I have picked up the VBA from an earlier thread (@SteynBS @Alex Blakenburg for that solution)
and previous work but I am struggling with some of the adaptations I need to make.
The specific issue I have is that the location of the file will change each time - I can get the exact location of the file into a cell on a worksheet - and need this code to open the log.txt file in that location.
The table the reference is in is called FilePath
The absolute reference is bg_Variables Cell F5
This is the code I am using
Thx
I have picked up the VBA from an earlier thread (@SteynBS @Alex Blakenburg for that solution)
and previous work but I am struggling with some of the adaptations I need to make.
The specific issue I have is that the location of the file will change each time - I can get the exact location of the file into a cell on a worksheet - and need this code to open the log.txt file in that location.
The table the reference is in is called FilePath
The absolute reference is bg_Variables Cell F5
This is the code I am using
Thx
VBA Code:
Sub ImportTextFileToExcel()
Dim textFileNum, rowNum, colNum As Integer
Dim textFileLocation, textDelimiter, textData As String
Dim tArray() As String
Dim sArray() As String
textFileLocation = "\\XXX-data1\XXXX_XXXXX\XXX_XXX_Requests\Done\2022\Oct2022\12Oct\XXXXXXXX20221012_20221012_113001\log.txt"
textDelimiter = "|"
textFileNum = FreeFile
Open textFileLocation For Input As textFileNum
textData = Input(LOF(textFileNum), textFileNum)
Close textFileNum
tArray() = Split(textData, vbLf)
Dim wb As Workbook: Set wb = ThisWorkbook
Dim shtDest As Worksheet
Dim lrowDest As Long
Set shtDest = wb.Sheets("LogFile")
For rowNum = LBound(tArray) To UBound(tArray) - 2
If Len(Trim(tArray(rowNum))) <> 0 Then
sArray = Split(tArray(rowNum), textDelimiter)
For colNum = LBound(sArray) To UBound(sArray)
shtDest.Cells(lrowDest + 2, colNum + 1) = sArray(colNum)
Next colNum
lrowDest = lrowDest + 1
End If
Next rowNum
End Sub