Using VBA to import a Text File to a Table

Whiter

New Member
Joined
Oct 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top