Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2023, 09:50 PM
14spar15 14spar15 is offline Copy and Paste certain columns from selected rows. Windows 7 64bit Copy and Paste certain columns from selected rows. Office 2010 64bit
Advanced Beginner
Copy and Paste certain columns from selected rows.
 
Join Date: Mar 2011
Posts: 97
14spar15 is on a distinguished road
Default Copy and Paste certain columns from selected rows.

Hello, From “Sheet1” I want to be able to select random rows and when the macro is run columns B, C, E, G and J from the selected rows will be pasted into the “Paste” sheet. I found this code in a forum that seems to work except for they wanted Rows A, B and E.



Sub Macro1()
Dim rng As Range
For Each rng In Selection.Areas
Union(rng.Resize(, 2), rng.Resize(, 1).Offset(, 4)).Copy Sheets("Paste").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Next rng
Worksheets("Paste").Activate
End Sub

I believe I understand the “Copy Sheets("Paste").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)” part and it works just as I need. I do not understand how “ Union(rng.Resize(, 2), rng.Resize(, 1).Offset(, 4)).” selects columns A,B and E and how to change it to get me to the results I need. Thanks


Note:
Union(rng.Resize(, 2), rng.Resize(, 1).Offset(, 4)).Copy Sheets("Paste").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Is one line.
Attached Files
File Type: xlsm Book4A.xlsm (20.2 KB, 2 views)
Reply With Quote
  #2  
Old 05-18-2023, 04:29 AM
p45cal's Avatar
p45cal p45cal is offline Copy and Paste certain columns from selected rows. Windows 10 Copy and Paste certain columns from selected rows. Office 2019
Expert
 
Join Date: Apr 2014
Posts: 866
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

This should make it a bit easier to choose which columns you want copying:
Code:
Sub blah()
Dim rng As Range
For Each rng In Selection.Areas
  With rng.EntireRow
    Union(.Columns("B:C"), .Columns("E:E"), .Columns("G:G"), .Columns("J:J")).Copy Sheets("Paste").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
  End With
Next rng
Worksheets("Paste").Activate
End Sub
This doesn't quite act in the same way as your original code which took horizontal offsets from the selection; this means that if an area's first column wasn't column A, the cells copied were offset from that column, not column A. If you want it still to operate that way, change the line:
With rng.EntireRow
to:
With rng

Note that whichever you choose above, you won't need to select ranges which cover all the columns you want to copy, only the leftmost column. Moreover, if you leave EntireRow in, it doesn't matter which column(s) you choose at all.
Reply With Quote
  #3  
Old 05-18-2023, 07:57 AM
14spar15 14spar15 is offline Copy and Paste certain columns from selected rows. Windows 7 64bit Copy and Paste certain columns from selected rows. Office 2010 64bit
Advanced Beginner
Copy and Paste certain columns from selected rows.
 
Join Date: Mar 2011
Posts: 97
14spar15 is on a distinguished road
Default

Thanks for the help here. Your code seems ideal for my application. The EntireRow tip was also very helpful. Thanks also for the explanation on the original code.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy unique worksheet to another workbook refuses says columns rows differ DBenz Excel 3 02-16-2023 07:45 AM
Copy and Paste certain columns from selected rows. Copy selected text from one document and paste to new document in same position on the page gasparik Word VBA 1 05-11-2020 05:41 AM
Copy and Paste certain columns from selected rows. How do I copy text from wxcel and paste to word , eliminating lines but keeping columns aligned? KPS2019 Word 4 05-31-2019 04:04 PM
how can i split a column of rows in half without copy and paste.. Lindseyfparker Excel 2 04-25-2014 12:52 AM
Copy and Paste certain columns from selected rows. Find specific rows then copy and paste to new doc konopca Word VBA 5 02-20-2014 02:34 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft