VBA Assistance Please!!

Desperationmode

New Member
Joined
Jan 8, 2019
Messages
3
[FONT=&quot]Hey Everyone,[/FONT]
[FONT=&quot]I have a workbook with 60+ worksheets, and inconsistent data ranges within each. I need to create VBA code to:[/FONT]

  • Search all worksheets in the workbook for specific text within a formula: Find = "*CODE"*
  • Replace any formula containing *CODE* with the value in the cell that the formula is calculating.
[FONT=&quot]There is a significant amount of data in these workbooks, so looping through cells would be inefficient and probably crash excel. Here's what I've got so far, a pretty basic find and replace... but is there any way to replace with values, versus another string!?[/FONT]
[FONT=&quot]Sub FindReplaceAll()[/FONT]
[FONT=&quot]Dim sht As Worksheet[/FONT]
[FONT=&quot]Dim fnd As Variant[/FONT]
[FONT=&quot]Dim rplc As Variant[/FONT]
[FONT=&quot]fnd = "*CODE*"[/FONT]
[FONT=&quot]'I'd like to replace any cell containing containing *CODE* in the formula, with the value that the formula is calculating[/FONT]
[FONT=&quot]rplc = "PLEASE BE VALUE IN CELL"[/FONT]
[FONT=&quot]For Each sht In ActiveWorkbook.Worksheets[/FONT]
[FONT=&quot]sht.Cells.Replace what:=fnd, Replacement:=rplc, _[/FONT]
[FONT=&quot]LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _[/FONT]
[FONT=&quot]SearchFormat:=False, ReplaceFormat:=False[/FONT]
[FONT=&quot]Next sht[/FONT]
[FONT=&quot]End Sub[/FONT]
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
welcome
Code:
Sub FindReplaceAll()
    
    Const IDENTIFIER As String = "CODE"
    
    Dim rng As Excel.Range
    Dim sht As Excel.Worksheet
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    'I'd like to replace any cell containing containing *CODE* in the formula, with the value that the formula is calculating
    For Each sht In ActiveWorkbook.Worksheets
        Set rng = sht.Cells.Find(What:=IDENTIFIER, LookIn:=xlFormulas, LookAt:=xlPart)
        Do While Not rng Is Nothing
            rng.Value = rng.Value
            Set rng = sht.Cells.FindNext
        Loop
    Next sht
    Set sht = Nothing
    
    Application.EnableEvents = True
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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