Copy data and paste everything

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have the following code and I want it to copy and paste values and format like cell size etc.

Code:
Sheets("home").Range("A1:E42").copy Destination:=Sheets(MonthYear).Range("A1")

What do I need to add to it as it only copies the values at the moment?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I have the following code and I want it to copy and paste values and format like cell size etc.

Code:
Sheets("home").Range("A1:E42").copy Destination:=Sheets(MonthYear).Range("A1")

What do I need to add to it as it only copies the values at the moment?

I don't know that there is a way to copy things like cell sizes.

What if you copied the whole worksheet tab to a new sheet tab and then named it 'MonthYear'?
 
Upvote 0
Well, how do I paste the formats too?
 
Last edited:
Upvote 0
What's wrong with this code?

Code:
    Sheets("home").Range("A1:E42").copy
    
    With Worksheets(MonthYear)
         .PasteSpecial Paste:=xlPasteValues
         .PasteSpecial Paste:=xlPasteFormats
    End With

as it has an error in the code and highlights
.PasteSpecial Paste:=xlPasteValues
 
Last edited:
Upvote 0
Maybe (untested)...

Code:
    Dim myRw As Long, myCo As Long
    Application.ScreenUpdating = False
    
    With Sheets("home").Range("A1:E42")
        .Copy Sheets(MonthYear).Range("A1")
        
        For myRw = 1 To .Rows.Count
            Sheets(MonthYear).Range("A1").Resize(.Rows.Count, .Columns.Count).Rows(myRw).RowHeight = .Rows(myRw).RowHeight
        Next myRw
        
        For myCo = 1 To .Columns.Count
            Sheets(MonthYear).Range("A1").Resize(.Rows.Count, .Columns.Count).Columns(myCo).ColumnWidth = .Columns(myCo).ColumnWidth
        Next myCo
    End With
    
    Application.ScreenUpdating = True

as it has an error in the code and highlights
.PasteSpecial Paste:=xlPasteValues
The error is because you haven't specified a range but it won't adjust the row/column heights anyway.

The code you had originally "pastes" the formats just not the cell height/width.
 
Last edited:
Upvote 0
I get an error with that saying subscript out of range and it highlights this row of code:

.copy Sheets(MonthYear).Range("A1")
 
Upvote 0
I get an error with that saying subscript out of range and it highlights this row of code:

.copy Sheets(MonthYear).Range("A1")

If MonthYear is the name of the sheet (not a variable) then:
Code:
.copy Sheets("MonthYear").Range("A1")
 
Upvote 0
If MonthYear is the name of the sheet (not a variable) then:
Code:
.copy Sheets("MonthYear").Range("A1")
@dpaton05, if what Akuini has put is correct (and I suspect he is) then please note that the reason I wrote it like that is because that is how you have referred to MonthYear in all the code you have posted in this thread, and so I have had to assume you had it correct as you haven't posted the full code that you are using.

If the above isn't correct then obviously you need to add your code for defining the variable to the code I posted,
 
Upvote 0
Month year is a variable, it contains the name of the sheet. I have this code working successfully to copy and paste the sheet but how do I rename it to MonthYear?

Code:
    ActiveSheet.copy After:=Worksheets(Worksheets.Count)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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