Problem With Paste ColumnWidths

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
Excel 2002, Windows XP

Hello all: I have a problem with the below code hanging on ( Selection.PasteSpecial Paste:=ColumnWidths). Why does it not let me copy the Column With? Basicaly the code inserts a new sheet and then copies data fron sheet (Main) to it. Thanks Dan


ActiveSheet.Unprotect
Range("A2").Select
Selection.Copy
Range("K6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A14").Select
Range("A14").Select
Range("B2:S60").Select
Selection.Copy
Sheets.Add
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False


Selection.PasteSpecial Paste:=ColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False



ActiveCell.Offset(12, 0).Range("A1:F1").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$S$59"
With ActiveSheet.PageSetup
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1

ActiveSheet.Range("J5").Activate
Selection.Copy
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("A1").Activate


Sheets("Main").Select
ActiveWindow.SmallScroll Down:=-51
Application.CutCopyMode = False
Range("A14").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I recorded doing what you're doing, and when I tried to run my macro with Paste:=xlcolumnwidths, it doesn't work. I get a "variable not defined" error. That's pretty weird. It's not in the Object Browser either.

However, you might try adjusting the column width of the destination to the column width of the source after you paste the data -- or simply .AutoFit the column.

Hope that helps!
 
Upvote 0
Thx Taz, The problem I think is that I do not know what sheet is being inserted, sheet1, 2 3 ect. I use the code on Excel XP and It works fine, but I dont know why it dont work on 2002 or 2003 version. Thanks Dan
 
Upvote 0
Maybe to resolve the problem, is there some code that would pick the last worksheet inserted in a workbook and then trying to run the code after that? Thanks Dan
 
Upvote 0
couple of things.....
shouldn't
ColumnWidths BE > xlColumnWidths

Also for versions prior to xl2003 ?? there was a bug.

To over come this Define;

xlPasteColumnWidths = 8
 
Upvote 0
Thanks Ivan for the advice seems to solve the problem: Running 2002 SP2 both of the below seem to work well; Will have to try on 2000 when I get a chance. Thanks again for your help in this mater. Dan

Selection.Columns.AutoFit

Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,981
Members
448,538
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top