简体   繁体   中英

Remove formulas from all worksheets in Excel using VBA

I'm having toubles removing formulas from cells in Excel and keep only the value (in case there is a number). The problem is due to the fact that there are also pivot tables (and also GETPIVOTDATA cells) within the different spreadsheets.

I am currently trying this code but it only works on normal spreadsheets:

Sub fun()

Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim pc As PivotCell

For Each ws In ThisWorkbook.Worksheets
    ws.Activate
    With ws.UsedRange
        .Value = .Value
    End With
Next ws


For Each ws In ThisWorkbook.Worksheets
    For Each pt In ws.PivotTables
        For Each pf In pt.PivotFields
            For Each pi In pf.PivotItems
                pi.Value = pi.Value
            Next pi
        Next pf
    Next pt
Next ws

End Sub

Could you help me to adapt the code so that every cell will be set to value?!

Option Explicit

Sub test1()
    Dim ws As Worksheet, a, area As String
    For Each ws In ThisWorkbook.Worksheets
        a = ws.UsedRange
        area = ws.UsedRange.Address
        ws.Cells.ClearContents
        ws.Range(area) = a
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM