3
votes

I'm trying to write a macro to loop through every worksheet in the active workbook, and save all formulas as values. To do this, for each sheet, first I run through each pivot table, then select the table and copy & paste as values. Then I am trying to use the worksheet.activerange.value = .value method to save the rest of the cells in the sheet.

I am getting a 1004 runtime error on the line wks.UsedRange.Value = wks.UsedRange.Value.

I have two questions:

1) How can I fix my runtime error?

2) Is there a way to get .value = .value to work with pivot tables? In previous macros this has never worked with pivots, so I have to use copy and paste as below.

Many thanks for your help!

Sub LockWorkbook()

Dim pvt As PivotTable
Dim wks As Worksheet
Dim i As Integer
Dim n As Integer

n = 1

For Each wks In ActiveWorkbook.Worksheets
    i = 1
    For Each pvt In wks.PivotTables
        wks.PivotTables(i).TableRange2.Select
        With Selection
            .Copy
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
        End With
        i = i + 1
        Next pvt

    Set wks = ActiveWorkbook.Worksheets(n)
    wks.UsedRange.Value = wks.UsedRange.Value
    n = n + 1

    Next wks

End Sub
2
Shouldnt it be usedrange.formula = usedrange.value. im sorry I might be wrong. - The King
Also, pivot tables cannot contain formulas. They can only contain values, so you should not need a macro like this. - RBarryYoung
Wouldnt cells.copy cells.pastespecial xlValues work for whole sheet including pivot - The King

2 Answers

1
votes

On The King's point, you can simply copy and paste the whole sheet as values.

Sheets("Sheet1").cells.copy
Sheets("Sheet1").cells.PasteSpecial Paste:=xlPasteValues

If you wanted to throw that in a simple loop to do it to all pages it can look like:

Sub Test1()

    Dim WS_Count As Integer
    Dim I As Integer


    WS_Count = ActiveWorkbook.Worksheets.Count

    For I = 1 To WS_Count

        ActiveWorkbook.Worksheets(I).Cells.Copy
        ActiveWorkbook.Worksheets(I).Cells.PasteSpecial Paste:=xlPasteValues

    Next I

End Sub

Please let me know if that helped!

0
votes

Latest edit eliminates Run-time error '1004' (excludes Pivot Tables from conversion to values)

Option Explicit

Public Sub ConvertFormulasToValuesInUsedRangeExceptPivotTables()
    Dim ws As Worksheet, ur As Range, cel As Range
    Dim pvt As PivotTable, nonPivot As Range, isPvt As Boolean

    For Each ws In ThisWorkbook.Worksheets
        Set ur = ws.UsedRange

        For Each cel In ur

            For Each pvt In ws.PivotTables      'excludes pivot tables
                isPvt = Not Intersect(cel, pvt.TableRange2) Is Nothing
                If isPvt Then Exit For
            Next

            If Not isPvt Then
                If Len(cel.Formula) > 0 Then    'excludes empty cells
                    If nonPivot Is Nothing Then
                        Set nonPivot = cel
                    Else
                        Set nonPivot = Union(nonPivot, cel)
                    End If
                End If
            End If

        Next cel

        nonPivot.Value = nonPivot.Value
        Set nonPivot = Nothing

    Next ws

End Sub