Can you please write me a vba code that will do the following: Copy A3:C23 in sheet…
- User
Can you please write me a vba code that will do the following:
Copy A3:C23 in sheet 'HolCard' Open a new NotePad And paste the range into the text document One Row after the another Row.
- Assistant
Sure! Here is the VBA code that will accomplish what you are looking for:
Sub CopyRangeToNotepad() Dim rng As Range Dim cell As Range Dim notepadPath As String ' Set the range to be copied Set rng = ThisWorkbook.Sheets("HolCard").Range("A3:C23") ' Open Notepad Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") ' Wait for Notepad to open ' Send the range data to Notepad For Each cell In rng SendKeys cell.Value & vbCrLf Next cell End SubTo run this code, you can go to the Developer tab in Excel, click on "Visual Basic" to open the VBA editor, and then paste this code into a new module. You can then run the
CopyRangeToNotepadsubroutine by clicking on the green play button or by assigning it to a button. - User
When it sends to notepad, I want A3:C3 to be on one line, the A2:C3 on the next and A4:C4 on the next nd so on
- Assistant
I apologize for the misunderstanding. Here is the updated VBA code to paste each row from range A3:C23 on a separate line in Notepad:
Sub CopyRangeToNotepad() Dim rng As Range Dim row As Range Dim notepadPath As String Dim notepadText As String ' Set the range to be copied Set rng = ThisWorkbook.Sheets("HolCard").Range("A3:C23") ' Open Notepad Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") ' Wait for Notepad to open ' Create the text to be pasted For Each row In rng.Rows For Each cell In row.Cells notepadText = notepadText & cell.Value & vbTab Next cell notepadText = Left(notepadText, Len(notepadText) - 1) & vbCrLf ' remove the last tab and add a newline Next row ' Send the text to Notepad SendKeys notepadText End SubThis code will loop through each row in the range A3:C23 and concatenate the values of each cell in the row with a Tab character. It will then remove the last Tab character and add a newline before moving to the next row. Finally, it will send the concatenated text to Notepad.