I would like to write a vba code that searches for all the text values "REP" in column…
- User
I would like to write a vba code that searches for all the text values "REP" in column C. For each value found, I would like to copy the row values of column A to K and paste each value into a seprate line in a new Notepad document
- Assistant
Here's the VBA code to achieve the desired outcome:
Sub CopyRowsIntoNotepad() Dim lastRow As Long Dim i As Long Dim filePath As String
'Get the last row of data in column C lastRow = Range("C" & Rows.Count).End(xlUp).Row 'Set the file path of the Notepad document filePath = "C:\Temp\NotepadDocument.txt" 'Open the Notepad document Open filePath For Output As #1 'Loop through each cell in column C For i = 1 To lastRow If Range("C" & i).Value = "REP" Then 'Copy the row values of column A to K Range("A" & i & ":K" & i).Copy 'Paste each value into a separate line in the Notepad document Print #1, Join(Application.Transpose(Application.Transpose(Range("A" & i & ":K" & i))), vbNewLine) End If Next i 'Close the Notepad document Close #1 'Notify the user the process is complete MsgBox "The rows containing 'REP' have been copied into Notepad.", vbInformationEnd Sub
Make sure to update the file path in the code to where you want to save the Notepad document. Also, ensure that the data range is correct for your sheet.
- User
Instead of opening a text document, I would prefer to create a new text document every time I run the code
- Assistant
Sure, you can modify the code to create a new text file each time you run the code. Here's the updated code:
Sub CopyRowsIntoNotepad() Dim lastRow As Long Dim i As Long Dim filePath As String Dim fso As Object Dim ts As Object
'Get the last row of data in column C lastRow = Range("C" & Rows.Count).End(xlUp).Row 'Create a new text file Set fso = CreateObject("Scripting.FileSystemObject") filePath = "C:\Temp\NotepadDocument" & Format(Now, "YYYYMMDDHHMMSS" & ".txt") Set ts = fso.CreateTextFile(filePath, True) 'Loop through each cell in column C For i = 1 To lastRow If Range("C" & i).Value = "REP" Then 'Copy the row values of column A to K Range("A" & i & ":K" & i).Copy 'Paste each value into a separate line in the text file ts.writeline Join(Application.Transpose(Application.Transpose(Range("A" & i & ":K" & i))), vbNewLine) End If Next i 'Close the text file ts.Close 'Notify the user the process is complete MsgBox "The rows containing 'REP' have been copied into the text file.", vbInformationEnd Sub
This code creates a new text file with a timestamp in the file name every time you run the code. Make sure to update the file path to the desired location on your computer.
- User
Can you adopt your code to make this code work: Sub CopyRowsIntoNotepad() Dim lastRow As Long Dim i As Long Dim filePath As String Dim fso As Object Dim ts As Object
'Get the last row of data in column C lastRow = Range("C" & Rows.Count).End(xlUp).Row
'Loop through each cell in column C For i = 1 To lastRow If Range("C" & i).Value = "REP" Then 'Copy the row values of column A to K Range("A" & i & ":K" & i).Copy
Shell "notepad.exe", vbNormalFocus SendKeys writeline Join(Application.Transpose(Application.Transpose(Range("A" & i & ":K" & i))), vbNewLine) End If Next i
Application.Wait Now + TimeValue("00:00:02") ThisWorkbook.Sheets(ActiveCell.Value).Activate
End Sub
- Assistant
Certainly! Here's the updated code that incorporates the Shell function and SendKeys method like you suggested:
Sub CopyRowsIntoNotepad() Dim lastRow As Long Dim i As Long Dim filePath As String Dim fso As Object Dim ts As Object
'Get the last row of data in column C lastRow = Range("C" & Rows.Count).End(xlUp).Row 'Loop through each cell in column C For i = 1 To lastRow If Range("C" & i).Value = "REP" Then 'Copy the row values of column A to K Range("A" & i & ":K" & i).Copy 'Open Notepad and paste the copied values Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:02") SendKeys "^V" SendKeys "{ENTER}" End If Next i 'Activate the sheet with the active cell value once the loop is finished Application.Wait Now + TimeValue("00:00:02") ThisWorkbook.Sheets(ActiveCell.Value).ActivateEnd Sub
The code opens Notepad, waits for 2 seconds, and then pastes the copied values. The "^V" keys simulate the Ctrl+V shortcut for pasting, and "{ENTER}" simulates pressing the Enter key after each value is pasted to create a new line. This code should achieve the desired outcome.