Solved

excel - paste special - column widths

Posted on 2001-06-13
8
297 Views
Last Modified: 2008-02-01
I have a worksheet that I'm selection a range with:
Sheets("Rate").Select
Range("Page").Select

I am copying it by:
selection.copy

I'm then creating a new workbook by:
Set newbook = Application.Workbooks.Add
newbook.Activate

My goal here is to paste the data in the range to the new sheet including formatting, column size, and values, but not with links.  The only way I have come close is to do this is:

    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Selection.PasteSpecial Paste:=xlColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

The problem is I get an error "Pastespecial method of range class failed" Runtime error "1004".  The other two work fine.  What do I need to do to get the column width to be copied?  Any help is greatly appreciated.  Thanks.
0
Comment
Question by:daron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 22

Expert Comment

by:Dreamboat
ID: 6188864
Daron, you can't do it when you manually paste, so I doubt you're going to get VBA to do it WITH the paste, so if you know the column width, you may need to add this line after you paste (because the pasted area should still be selected, right?)

    Selection.ColumnWidth = 15   (15 = column width ##)
0
 

Author Comment

by:daron
ID: 6188918
The range being selected is something like A5:T87.  I recorded a macro, selected this manually, did edit->copy,
then file->new->workbook, then edit->pastespecial->columnwidths, then edit->pastespecial->formats, then edit->pastespecial->values and this worked.  But if you try to run the code it produces, it hangs on the column width line.  Here is the code I generated when I recorded the above steps:

    Range("A5:T87").Select
    ActiveWindow.SmallScroll Down:=-45
    Selection.Copy
    Workbooks.Add
    Selection.PasteSpecial Paste:=xlColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False

I'm getting the formats and values to work in the code, but not the columnwidths.
Thanks.
0
 

Author Comment

by:daron
ID: 6188986
I think this is the solution:
http://support.microsoft.com/support/kb/articles/Q231/0/90.ASP?LN=EN-US&SD=gn&FR=0&qry=xlColumnWidths&rnk=1&src=DHCS_MSPSS_gn_SRCH&SPR=MSALL

There is an error in the constant microsoft has for the columnwith. setting it to 8 will correct this.


THanks for the help.  I want to delete this question.  I'll post another question and throw you some points for the assistance.  Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:Dreamboat
ID: 6190338
daron: you've posted your own solution; no need to give points. Go to community support (link at left) and ask them to put this question in the PAQ so others will find your solution when they search. CS will give your points back. Save your points for when you REALLY need them! LOL
0
 
LVL 13

Expert Comment

by:cri
ID: 6190626
Be aware that this will only work on Excel 2000, for Excel 97 you will have to use an array to store the column widths.
0
 
LVL 6

Expert Comment

by:blakeh1
ID: 6191056
Normally in excel if you copy the whole column and paste you get the column width.
you could try something like the following will retain column widths (at least it did when I tested it). Of course this will only work if you are pasting starting at row 1. If not it will fail.

Sheets("Sheet1").Activate
Range("A1:B1").EntireColumn.Select
Selection.Copy
Sheets("Sheet2").Activate
Range("A1").Select
   
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
       False, Transpose:=False
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
       False, Transpose:=False


0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6195245
daron has requested that this be PAQs vs. awarded, since the answer was found outside this process.

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 6195271
Points have been refunded, question moved to PAQ.

Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 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