Copy sheet and preserve formatting

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
370
I am using the below line of code to repeatedly copy a sheet over another sheet called "FINALORDER". But the formatting is not getting copied as I lose the column widths. What additional code do I need to accomplish this? Thanks

Sheets(Result).UsedRange.Copy Sheets("FINALORDER").Cells(1, 1)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
For the column width, I believe the below should work:

Code:
Sheets("Result").UsedRange.Copy
    Sheets("FINALORDER").Cells(1, 1).PasteSpecial xlPasteColumnWidths
 
Upvote 0
USe

Code:
Sub MM1()
Sheets("Result").UsedRange.Copy
With Sheets("FINALORDER")
    .Cells(1, 1).PasteSpecial xlPasteAll
    .Cells(1, 1).PasteSpecial xlPasteColumnWidths
End With
End Sub
 
Upvote 0
Hi I inserted your code into mine - see below, but I get a runtime error 9 and subscript out of range. The line "Sheets("Result").UsedRange.Copy" is hi-lighted with the error. Thanks

Sub COPY_SHEET_TO_FINALORDER()
Dim response As Integer
Dim Result As String
Dim ws As Worksheet

Result = InputBox("ENTER THE SHEET NAME YOU WANT TO COPY.")
If Result = "" Then Exit Sub

'check if Result exists
On Error Resume Next 'suppress error message if sheet doesn't exist
Set ws = Sheets(Result)
On Error GoTo 0 're-enable error notification

If ws Is Nothing Then
MsgBox "Sorry, there was no sheet " & Result & " found."
Exit Sub
Else
response = MsgBox("You are about to copy sheet" & Result & " to FINALORDER" & vbLf & _
"Select *YES* to continue or *NO* to quit.", vbYesNo)

If response <> vbYes Then Exit Sub
End If

Application.CopyObjectsWithCells = False

Sheets("Result").UsedRange.Copy
With Sheets("FINALORDER")
.Cells(1, 1).PasteSpecial xlPasteAll
.Cells(1, 1).PasteSpecial xlPasteColumnWidths
End With

End Sub
 
Upvote 0
Why not just copy the whole sheet all at once
Try this:
Code:
Sub Copy_Sheet()
'Modified  12/25/2018  11:40:55 PM  EST
Sheets("Result").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "FINALORDER"
End Sub
 
Upvote 0
Hi I inserted your code into mine - see below, but I get a runtime error 9 and subscript out of range. The line "Sheets("Result").UsedRange.Copy" is hi-lighted with the error. Thanks

Remove the quotes around "Result" as Result is a variable not a sheet name.

Edit:
Or change
Code:
Sheets("Result").UsedRange.Copy
to
Code:
ws.UsedRange.Copy
as you are also assigning the variable string to a worksheet variable in the code (no idea why you are doing both).
 
Last edited:
Upvote 0
Hi I inserted your code into mine - see below, but I get a runtime error 9 and subscript out of range. The line "Sheets("Result").UsedRange.Copy" is hi-lighted with the error. Thanks

Sub COPY_SHEET_TO_FINALORDER()
Dim response As Integer
Dim Result As String
Dim ws As Worksheet

Result = InputBox("ENTER THE SHEET NAME YOU WANT TO COPY.")
If Result = "" Then Exit Sub

'check if Result exists
On Error Resume Next 'suppress error message if sheet doesn't exist
Set ws = Sheets(Result)
On Error GoTo 0 're-enable error notification

If ws Is Nothing Then
MsgBox "Sorry, there was no sheet " & Result & " found."
Exit Sub
Else
response = MsgBox("You are about to copy sheet" & Result & " to FINALORDER" & vbLf & _
"Select *YES* to continue or *NO* to quit.", vbYesNo)

If response <> vbYes Then Exit Sub
End If

Application.CopyObjectsWithCells = False

Sheets("Result").UsedRange.Copy
With Sheets("FINALORDER")
.Cells(1, 1).PasteSpecial xlPasteAll
.Cells(1, 1).PasteSpecial xlPasteColumnWidths
End With

End Sub

Sheets("Result") should not be in quotes. It should be Sheets(Result)
 
Upvote 0
That worked thanks. One more thing...after it runs the "FINALORDER" sheet is darkened after the paste. That didn't happen before. Is there a way to get rid of that without having to click on a cell? Thanks
 
Upvote 0
That worked thanks. One more thing...after it runs the "FINALORDER" sheet is darkened after the paste. That didn't happen before. Is there a way to get rid of that without having to click on a cell? Thanks

You could put after the ".Cells(1, 1).PasteSpecial xlPasteColumnWidths" line

.Cells(1, 1).Select
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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