vba code to copy worksheets and paste as values.

randyharris

Board Regular
Joined
Oct 6, 2003
Messages
88
I know that I could do this for a specific workbook, but it would be less efficient as a recorded macro, and it wouldn't work for any workbook.

I have the need to start a macro that goes through all worksheets in a workbook (it can ignore hidden worksheets) and select the entire worksheet, copy, then paste as values.

So it would start at the first say sheet1, copy the whole sheet, paste as values, then go to sheet2, copy, paste as values, etc..

Any help would be appreciated.

Randy
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this:

Code:
Sub Test()
    Dim Sh As Worksheet
    For Each Sh In ThisWorkbook.Worksheets
        If Sh.Visible = True Then
            Sh.Activate
            Sh.Cells.Copy
            Sh.Range("A1").PasteSpecial Paste:=xlValues
            Sh.Range("A1").Select
        End If
    Next Sh
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Dear Mr. AndrewI am new to vba and want to do almost the same. the difference is I want to copy only two worksheets in my source and paste it in a new workbook as values. Could you kindly help me.
 
Upvote 0
Try:

Code:
Sub Test()
    With ThisWorkbook
        .Worksheets("SMOE-FRONT").Copy
        ActiveSheet.Cells.Copy
        ActiveSheet.Range("A1").PasteSpecial Paste:=xlValues
        .Worksheets("SMOE-BACK").Copy After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
        ActiveSheet.Cells.Copy
        ActiveSheet.Range("A1").PasteSpecial Paste:=xlValues
    End With
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Hi Andrews,

I tried running this script after protecting my sheets. But they dont seem to work and give me error "object disconnected from the client". What is the reason for this? i tried umprotecting the sheet but still doesn't work
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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