Solved

excel - paste special - column widths

Posted on 2001-06-13
8
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Expert Comment

by:cri
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Points have been refunded, question moved to PAQ.

Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: dandraka
There are several quick shortcuts that can make your life easier in Microsoft Programs.  These simple tips and tricks will your work more productive and you faster at completing your tasks! MS Word (1) Creating Re-usable Scraps You can create s…
In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

763 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

13 Experts available now in Live!

Get 1:1 Help Now