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
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
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!
 

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
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
 

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
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
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
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
 

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
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
 

Watch MrExcel Video

Forum statistics

Threads
1,118,055
Messages
5,569,946
Members
412,299
Latest member
agentless
Top