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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
here is my code. i have added other modules inside the code to suit my other needs too. the same code in the other unprotected version ran without any trouble.

Code:
Sub Test()


Dim DestinationBook As Workbook
Dim strSaveName As String
Dim ws As Worksheet
Dim collValues As Collection
Dim rCell As Range
Dim i As Integer, icolumn As Integer
 


    With ThisWorkbook
' my function to unprotect the sheet
    ActiveWorkbook.UnProtect
        .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
    Windows(GeneratorFile).Activate
    ActiveWorkbook.Protect
    'get the name to save
    Windows(GeneratorFile).Activate
    strSaveName = Worksheets("Configuration").Range("I56").Value
    Application.Dialogs(xlDialogSaveAs).Show (strSaveName)
    
    'This block reactivates the string counting in the label text
    Sheets("SMOE-FRONT").Select
    Range("T24:T25").Select
    ActiveCell.FormulaR1C1 = "=LEN(RC[-10])"
    Range("T24:T25").Select
    ActiveWindow.SmallScroll Down:=6
    Selection.AutoFill Destination:=Range("T24:T41"), Type:=xlFillDefault
    Range("T24:T41").Select
    ActiveWorkbook.Save
   
'To remove the blanks between lines
Set ws = Sheets("SMOE-FRONT")
For icolumn = 3 To 8
    If Application.WorksheetFunction.CountA(ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn))) > 1 Then
        Set collValues = New Collection
        For Each rCell In ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn)).SpecialCells(xlCellTypeConstants)
            collValues.Add rCell.Value
        Next rCell
        ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn)).ClearContents
        For i = 1 To collValues.Count
            ws.Cells(53, icolumn).End(xlUp).Offset(1, 0).Value = collValues.Item(i)
        Next i
        Set collValues = Nothing
    End If
Next icolumn
    Application.CutCopyMode = False
    'end of code to remove blanks
    ActiveWorkbook.Save
    ActiveWindow.Close
Windows(GeneratorFile).Activate
ActiveWorkbook.protect
Sheets("Selector Homepage").Select
End Sub
 
Upvote 0
Instead of:

Code:
ActiveWorkbook.Unprotect

I expect you need you need eg:

Code:
.Worksheets("SMOE-FRONT").Unprotect
 
Upvote 0
I am a newbiee with no VBA experince. I have the following code that copies all sheets and then paste it as values. I would some assistance if someone could please modify the code to unhide all hidden columns first and then past all as values. If possible, I would then like it to delete all sheets other than the one that I have selected.

Any help would be greatly appreciated!

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
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


Hi Andrew,

May you please help me. I am new to VBA and want to do the same but paste the copy in the same workbook but different sheet with a specific name let's say tab "T-1".

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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