Solved

Copy and Paste Columns

Posted on 2014-09-16
2
208 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
 
LVL 1

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 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