Private Sub CommandButton2_Click() Dim fs As Object, folder As Object, file As Object…
- User
Private Sub CommandButton2_Click() Dim fs As Object, folder As Object, file As Object Dim wbPath As String, ws As Worksheet Dim i As Integer
' Set the worksheet object to the active sheet Set ws = ThisWorkbook.Sheets("Sheet1")
' Initialize the FileSystemObject Set fs = CreateObject("Scripting.FileSystemObject")
' Get the folder where this workbook is saved Set folder = fs.GetFolder(ThisWorkbook.Path)
' Initialize row index for Sheet1 column G i = 4
code' Loop through each file in the directory For Each file In folder.Files If LCase(fs.GetExtensionName(file.Name)) = "txt" Then ' Open the text file textFile = FreeFile Open file.Path For Input As textFile fileContent = Input(LOF(textFile), textFile) Close textFile ' Use Regular Expression to find the numeric value after "Placed" Set RegEx = CreateObject("VBScript.RegExp") With RegEx .Global = False .IgnoreCase = True .Pattern = "Placed\D*(\d+)" End With ' If a matching pattern is found, extract the numeric value If RegEx.Test(fileContent) Then numericValue = RegEx.Execute(fileContent)(0).SubMatches(0) ' Write the numeric value into column G starting from row 4 ws.Cells(i, 7).Value = numericValue i = i + 1 End If ' Clean up RegExp object Set RegEx = Nothing End If Next file ' Clean up FileSystemObject Set fs = NothingEnd Sub add to the code this : before to paste numeric value in column g first search for .txt file names in this file and paste numerical value only on corespondig row on column g . if name of file was not found then msg box file found , if not file not found
- Assistant