I am trying to create a macro that copies cells B5:C5, C6:C7, and B8:C23 into notepad. However, whenever I paste, it also includes cells B6 & B7. How do I prevent this. I did not select them initially.
Thank you!
I am trying to create a macro that copies cells B5:C5, C6:C7, and B8:C23 into notepad. However, whenever I paste, it also includes cells B6 & B7. How do I prevent this. I did not select them initially.
Thank you!
Hello kati8293,
Welcome to the Forum!
When the columns are pasted do you want the values to be in 2 columns separated by spaces or tabs or what?
Do you want an empty field (blank) pasted for cells B6:B7 or paste C6:C7 in the first field position?
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thank you for the welcome Leigh.
Yes, I would like the values to be in 2 columns seperated by spaces with cells B6 & B7 blank. If possible, if the C column is left blank, we want to skip that cell and the B cell beside it as well.
Seems very odd that it just adds cells that were not selected when I go to paste.
Hello kati8293,
This macro will copy the data directly to a file and save it. This is easier than first opening Notepad and copying the data to it and closing. Change the file path and name to want you will be using. This is marked in bold text in the macro code.
![]()
'Thread: http://www.excelforum.com/excel-programming/784879-how-to-copy-non-contiguous-cells-to-notepad.html 'Poster: kati8293 'Written: July 19, 2011 'Author: Leith Ross Sub CopyRangeToTextFile() Dim Cell As Range Dim Data As Variant Dim Filespec As String Dim N As Integer Dim rngArea As Range Dim rngRow As Range Dim Rng As Range Filespec = "C:\Test File.txt" N = FreeFile Set Rng = ActiveSheet.Range("B5:C5,C6:C7,B8:C23") For Each rngArea In Rng.Areas For Each rngRow In rngAreas.Rows Data = rngRow.Cells.Value Open Filespec For Output As #N If Data(1, 2) = "" Then Data(1, 1) = "" Print #N, Data(1, 1), Spc(1), Data(1, 2) Close #N Next rngRow Next rngArea End Sub
Adding the Macro (Excel 95 - 2003)
- Copy the macro above pressing the keys CTRL+C
- Open your workbook
- Press the keys ALT+F11 to open the Visual Basic Editor
- Press the keys ALT+I to activate the Insert menu
- Press M to insert a Standard Module
- Paste the code by pressing the keys CTRL+V
- Make any custom changes to the macro if needed at this time.
- Save the Macro by pressing the keys CTRL+S
- Press the keys ALT+Q to exit the Editor, and return to Excel.
To Run the Macro...
To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
or
![]()
Sub snb() sn = Range("B5:C23") For j = 1 To UBound(sn) c01 = c01 & vbCrLf & iif(sn(j,2)="","",IIf(j <> 2 And j <> 3, sn(j, 1), "") & vbTab & sn(j, 2)) Next Open "E:\OF\example.txt" For Output As #1 Print #1, Mid(c01, 3) Close End Sub
Last edited by snb; 07-20-2011 at 05:18 AM.
Leigh, it did not work. It errored here "For Each rngRow In rngAreas.Rows"
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks