Solved

Copy and Paste Columns

Posted on 2014-09-16
2
206 Views
Last Modified: 2014-09-17
I found this code on the Internet and it works well but I'd like to specify the ranges to copy to a new sheet (which are columns). So, instead of wsSource.Range("E9:Q351").Copy, I'd like to select different columns that start at a specific row. Like, wsSource.Range("column A10:A50, column F10:F50, column S10:S50, etc...").Copy. I hope this makes sense. Thanks!


Sub Foo()
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wbDest As Workbook
Dim fName As String

'References
Set wbSource = ActiveWorkbook
Set wsSource = ActiveSheet
Set wbDest = Workbooks.Add

'Copy range on original sheet
wsSource.Range("E9:Q351").Copy

'----------------------------
'Save in new workbook
wbDest.Worksheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False

'Get file name and location from user
fName = Application.GetSaveAsFilename(FileFilter:="CSV (*.csv), *.csv", Title:="Save As")
If fName = "" Then Exit Sub '//user cancelled

'Save new csv file
wbDest.SaveAs fName, xlCSV

wbDest.Close SaveChanges:=True
'----------------------------

End Sub

Open in new window

0
Comment
Question by:tracyms
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40327291
You just do it like this:
wsSource.Range("A10:A50,F10:F50,S10:S50").Copy

Open in new window

i.e. just comma-separate the ranges. Make sure that the ranges do not intersect
0
 

Author Comment

by:tracyms
ID: 40328152
Geez! I tried that but had it like this wsSource.Range("A10:A50","F10:F50","S10:S50").Copy - quotes around each range and now I see why it wasn't working! Thank you! :-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now