Solved

excel - paste special - column widths

Posted on 2001-06-13
8
295 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

860 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