Remove formula only visible cells

Rpaikh

New Member
Joined
Jul 28, 2019
Messages
27
Hello,

I basically use below code to remove formula for the entire sheets.

But is anyone happen to know if we could have a VBA code to remove formula only the selected visible cells ?
Sometimes I need to keep some formula by hiding or filter it out.




Code:
Sub Removeformu()
    Dim ws As Worksheet


    Set ws = ThisWorkbook.Sheets("SPI)


    ws.UsedRange.Value = ws.UsedRange.Value
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
One way (Filtered cells)

Code:
Sub Rpaikh()
    Dim Cel As Range, Rng As Range
    Set Rng = Sheets("SPI").UsedRange.SpecialCells(xlCellTypeVisible)
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
            For Each Cel In Rng
                Cel.Value = Cel.Value
            Next
        .Calculation = xlCalculationAutomatic
    End With
End Sub


If you want to filter AND then apply to manually selected range of visible filtered cells
instead of
Code:
 Set Rng = Sheets("SPI").UsedRange.SpecialCells(xlCellTypeVisible)
use
Code:
 Set Rng = Selection.SpecialCells(xlCellTypeVisible)
 
Last edited:
Upvote 0
Thank very much Yongle.


This perfectly work as expected !


One way (Filtered cells)

Code:
Sub Rpaikh()
    Dim Cel As Range, Rng As Range
    Set Rng = Sheets("SPI").UsedRange.SpecialCells(xlCellTypeVisible)
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
            For Each Cel In Rng
                Cel.Value = Cel.Value
            Next
        .Calculation = xlCalculationAutomatic
    End With
End Sub


If you want to filter AND then apply to manually selected range of visible filtered cells
instead of
Code:
 Set Rng = Sheets("SPI").UsedRange.SpecialCells(xlCellTypeVisible)
use
Code:
 Set Rng = Selection.SpecialCells(xlCellTypeVisible)
 
Upvote 0
Hello Yongle - Just one quick question


For .Calculation = xlCalculationManual and .Calculation = xlCalculationAutomatic


Is this just to speed up loops of Cel.Value = Cel.Value for the Rng range am I correct ?
 
Upvote 0
For .Calculation = xlCalculationManual and .Calculation = xlCalculationAutomatic
Is this just to speed up loops of Cel.Value = Cel.Value for the Rng range am I correct ?

The loop in the code means that values are being amended ONE cell at a time
(By default) a workbook auto-recalculates formulas EVERY time a value in any cell changes
Setting calculation mode to manual avoids those muliple recalculations
Setting calculation mode back to automatic (after ALL values have been amended) puts calculation mode back to normal and results in only ONE recalculation event
So, yes it speeds up the loop

Setting ScreenUpdating to False prevents the screen from updating until the sub finishes running. ONE update instead of EVERY time any cell changes. Again speeding up the loop. Screen updating does not require resetting in the code - that happens automatically.
 
Last edited:
Upvote 0
Thank you once again for a clear explanation.



The loop in the code means that values are being amended ONE cell at a time
(By default) a workbook auto-recalculates formulas EVERY time a value in any cell changes
Setting calculation mode to manual avoids those muliple recalculations
Setting calculation mode back to automatic (after ALL values have been amended) puts calculation mode back to normal and results in only ONE recalculation event
So, yes it speeds up the loop

Setting ScreenUpdating to False prevents the screen from updating until the sub finishes running. ONE update instead of EVERY time any cell changes. Again speeding up the loop. Screen updating does not require resetting in the code - that happens automatically.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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