×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Copy Excel Columns to New Workbook

Copy Excel Columns to New Workbook

Copy Excel Columns to New Workbook

(OP)
Hi All,

I am trying to copy 3 columns from an existing workbook and paste those columns to a NEW workbook, but I am getting errors that the method of PasteSpecial failed.

Here is what I have right now, which does seem to create the new workbook and paste the values in, but then fails to save and close. This also only works if I use 'On Error Resume Next' (Workbook 'A' is existing).

CODE

Set objExcel = CreateObject("Excel.Application")
strFileName = "C:\Scripts\B.xlsx"
objExcel.Visible = True

Set objNewFile = objExcel.Workbooks.Add()
objNewFile.SaveAs(strFileName)


Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\A.xlsx")
'Set objNewFile = objExcel.Workbooks.Open("C:\Scripts\B.xlsx")

Set objWorksheet = objWorkbook.Worksheets(1)

Set objRange = objWorksheet.Range("A:C").EntireColumn
objRange.Copy

Set objWorksheet2 = objNewFile.Worksheets(1)

objWorksheet.Range("A:C").Copy objWorksheet2.Range("A:C")
objWorksheet2.Range("A:C").PasteSpecial xlValues

objWorkBook.Close
objNewFile.Save
objNewFile.Close 

RE: Copy Excel Columns to New Workbook

Hi,

xlValues is an Excel Application constant, that you don't have library access to in VBScript.

https://docs.microsoft.com/en-us/office/vba/api/ex...

xlValues is -4163

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close