Solved

excel - paste special - column widths

Posted on 2001-06-13
8
293 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

914 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

20 Experts available now in Live!

Get 1:1 Help Now