I am able to copy the data from Text file into the Excel file using below mentioned code. However I would like to ask if we can add the following options while copying data from text to excel.
If we can add an option where we can write the Text file name in the code as well. The reason is that there are multiple text files in a FolderLocation and i am not able to select the specific text file from which data needs to be copied.
Currently it copies all the data from text file, is there any way we can add a criteria or date option in the code so that rather than selecting all the data it could select the data for certain date(s). The data in the text file is like mentioned below
[03] Sat 07Jan23 10:10:58 - Initializing
[03] Sat 07Jan23 10:10:58 - Selected key
[03] Sat 07Jan23 10:10:58 - Host
[03] Sat 07Jan23 10:10:58 - Server
[03] Sat 07Jan23 10:10:58 - Client
[07] Tue 10Jan23 06:51:02 - SSH
[08] Tue 10Jan23 06:51:02 - SSH
03] Tue 10Jan23 06:51:02 -
[07] Tue 10Jan23 06:51:02 -
The data in the log file is for multiple dates, i wish if we can copy the data of certain dates, e.g. if i write "07Jan23" date in the code it only copies all the complete rows for 07Jan23.
Sub ImportTextFileDatatoExcel()
Dim fileLocation As String, textData As String
Dim rowNum As Long
folderLocation = "E:\Logs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderLocation)
rowNum = 1
Close #1
For Each textFile In folder.Files
fileLocation = folder & "\" & textFile.Name
Open fileLocation For Input As #1
Do While Not EOF(1)
Line Input #1, textData
textData = Replace(textData, ";", ",")
If InStr(textData, ",") = 0 Then
Cells(rowNum, 1) = textData
Else
tArray = Split(textData, ",")
nColumn = 1
For Each element In tArray
Cells(rowNum, nColumn) = element
nColumn = nColumn + 1
Next element
End If
rowNum = rowNum + 1
Loop
Close #1
Next textFile
End Sub
i shall remain thankful