?
Solved

excel - paste special - column widths

Posted on 2001-06-13
8
Medium Priority
?
298 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
Office 365 Training for IT Pros

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.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 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