Macro to copy and paste values for every cell containing a specific formula (i.e sumif, round or vlookup)

phillipsdp

New Member
Joined
Feb 24, 2014
Messages
8
Hi -

I currently have a working macro but need help to make it loop through every sheet rather than only working on the active sheet. below is my current macro if you could help that would be GREAT!


Sub CPV()

Set Rng = Cells.Find(What:="round", _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not Rng Is Nothing Then
Do
Rng.Formula = Rng.Value
Set Rng = Cells.FindNext(Rng)
Loop Until Rng Is Nothing
End If
End Sub
 
Normally, to loop through all sheets in a workbook you would use:
Code:
Dim sh As Worksheet
For Each sh In Worksheets
    'do something here
    msgbox sh.Name
Next sh
 
Upvote 0
Yes I'm familiar with the common sheet loop. I just haven't been able to figure out how to use it in conjunction with this formula.
 
Upvote 0
I've added to the original formula to be able to replace multiple specified formulas at once. But still haven't cracked how to get it to loop through the workbook.

Sub AB()
Dim ws As Worksheet
Dim WhatToFind As Variant
Dim iCtr As Long


WhatToFind = Array("round", "sumif", "sumifs", "vlookup", "sumproduct", "today")
For iCtr = LBound(WhatToFind) To UBound(WhatToFind)
With rng

Set rng = Cells.Find(What:=WhatToFind(iCtr), _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not rng Is Nothing Then
Do
rng.Formula = rng.Value
Set rng = Cells.FindNext(rng)
Loop Until rng Is Nothing

End If
End With
Next
End Sub
 
Upvote 0
With what you're looking to do, I used ws.Select. Here's what I did with a test workbook. I put random formulas and values across 5 sheets in the workbook and was able to convert only the cells that contained a formula from the "WhatToFind" array.
Code:
Sub AB()
Dim ws As Worksheet
Dim WhatToFind As Variant
Dim iCtr As Long

WhatToFind = Array("round", "sumif", "sumifs", "vlookup", "sumproduct", "today")
For iCtr = LBound(WhatToFind) To UBound(WhatToFind)
    [COLOR=#ff0000]For Each ws In Worksheets
        ws.Select[/COLOR]
        Set Rng = Cells.Find(What:=WhatToFind(iCtr), _
            After:=[COLOR=#ff0000][A1][/COLOR], LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)
        If Not Rng Is Nothing Then
            Do
                Rng.Formula = Rng.Value
                Set Rng = Cells.FindNext(Rng)
            Loop Until Rng Is Nothing
        End If
    [COLOR=#ff0000]Next ws
Next iCtr[/COLOR]
End Sub
 
Upvote 0
Your code works, but not completely. I work with large databooks with tons of worksheets. I think the last thing to do is to put in some sort of error check that keeps it from getting stuck when nothing is found within the first 300 rows to help speed it up and keep it from hanging on a worksheet that potentially has a large range due to a user mistake.
 
Upvote 0

Forum statistics

Threads
1,226,835
Messages
6,193,232
Members
453,781
Latest member
Buzby

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