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 ObjectDim 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
' 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 FOR THIS CODE I WANT FIRST TO SEARCH IN FILE FOR SAME NAME AS .TXT FILE FROM WHERE WE COPY NUMBER AFTER Placed: and on exact same row Write the numeric value into column G starting from row 4
- Assistant
- User
This code will loop through each cell in column A starting from row 4-start with column B
- Assistant