I’m trying to take statistics of a specific column in a text file and I thought the best way to do it might be to copy all contents from the text file into an excel worksheet and then count it from there (otherwise I would need to try and read only that one line directly from the excel file). Here’s the code of what I have so far:
Dim filePath As String
Dim currentValue As String
Dim iRow As Long
Dim iCol As Long
Dim badAddress As Long
Dim coverageNoListing As Long
Dim activeListing As Long
Dim noCoverageNoListing As Long
Dim inactiveListing As Long
Dim fso As Object
Dim f As Object
'' filePath would include entire file name (picked from a browser button)
filePath = ActiveSheet.Range("B2").Text
'' Makes sure there isn't a sheet named "Temp_Text_File"
For Each testSheet In ActiveWorkbook.Worksheets
If testSheet.Name Like "Temp_Text_File" Then flag = True: Exit For
Next
'' If there is a sheet named "Temp_Text_File" it will be deleted
If flag = True Then
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("Temp_Text_File").Delete
Application.DisplayAlerts = True
End If
'' Recreate sheet
Sheets.Add.Name = "Temp_Text_File"
'' Here I would want to copy everything (similar to manually doing "Ctrl+A" then "Ctrl+C") from the text file
'' Then paste into worksheet (similar to manually doing "Ctrl+V") within this created worksheet range("A1")
'' Delete at the end (user has no need for it)
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("Temp_Text_File").Delete
Application.DisplayAlerts = True
Thank you,
Jesse Smothermon
I am doing a similar thing, here is my sub for this:
I open a txt file with
|as separator. Then copy the content of the sheet to my destination workbook (global variable). Then I close the first workbook that contains the original txt file without saving.The code for the Workbooks.OpenText is basically from recording a macro and adapting it to my needs.