Vba change multiple formulas

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows
Hi i have lots of different formulas with range 2 to 77777
Is it possible using a vba to change the 77777 to 177777

example
=COUNTIFS($C$2:$C$77777,C161664) would change to
=COUNTIFS($C$2:$C$177777,C161664)

Thanks
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,779
Office Version
  1. 365
Platform
  1. Windows
Use Find & Replace.
Under "Options" select the "Formulas" check box next to "Look in."

The only issue is, I'm not sure whether or not there's a way to prevent cell contents with the "find" value, being replaced, too. What you could do, though, is to use "Find next" to iterate through them all one-by-one, and use "Replace" each time you get to a formula...

Edit - Sorry, didn't notice your "VBA" requirement...
 

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows
Use Find & Replace.
Under "Options" select the "Formulas" check box next to "Look in."

The only issue is, I'm not sure whether or not there's a way to prevent cell contents with the "find" value, being replaced, too. What you could do, though, is to use "Find next" to iterate through them all one-by-one, and use "Replace" each time you get to a formula...

iv tried using find and replace, but i have that much data on sheet , that it would take around 16 to 24 hours to complete,
I wonder if a vba would be quicker
Thanks
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,779
Office Version
  1. 365
Platform
  1. Windows
Try this...

NB - test on a COPY of your data, first!!!
VBA Code:
Sub fnd_rep()
Dim cl As Range

Application.ScreenUpdating = False

    On Error Resume Next
 
    For Each cl In Me.UsedRange.SpecialCells(xlFormulas)
        cl.Formula = Replace(cl.Formula, "77777", "177777")
    Next cl

Application.ScreenUpdating = True
End Sub
Edit - missed one of the 7s from the "replace" part!
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,779
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

...It would be quicker to amend the code, to only look at those ranges in which you knew the replacements would be needed - i.e. only certain columns, for example.
 

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows
Try this...

NB - test on a COPY of your data, first!!!
VBA Code:
Sub fnd_rep()
Dim cl As Range

Application.ScreenUpdating = False

    On Error Resume Next

    For Each cl In Me.UsedRange.SpecialCells(xlFormulas)
        cl.Formula = Replace(cl.Formula, "77777", "177777")
    Next cl

Application.ScreenUpdating = True
End Sub
Edit - missed one of the 7s from the "replace" part!

Thanks for your help
but i get compile error
invalid use of Me keyword
 

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

...It would be quicker to amend the code, to only look at those ranges in which you knew the replacements would be needed - i.e. only certain columns, for example.

yes using find and replace i only highlighted the columns that ranges where in, but would still take along time to complete. thanks
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,779
Office Version
  1. 365
Platform
  1. Windows
...You'll need to place the code in the worksheet module, pertinent to the sheet in question.
Either that, or reference the full sheet name in question, thus:
VBA Code:
Sub fnd_rep()
Dim cl As Range

Application.ScreenUpdating = False

    On Error Resume Next

    For Each cl In Sheets("Sheet1").UsedRange.SpecialCells(xlFormulas)
        cl.Formula = Replace(cl.Formula, "77777", "177777")
    Next cl

Application.ScreenUpdating = True
End Sub
... replace "Sheet1" with the sheet name, in question.
Edit - sorry - missed a double quote off "Sheet1." Corrected now.
 

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
       Sub fnd_rep()
Dim cl As Range

Application.ScreenUpdating = False

On Error Resume Next

For Each cl In Sheets(data).UsedRange.SpecialCells(xlFormulas)
cl.Formula = Replace(cl.Formula, "77777", "177777")
Next cl

Application.ScreenUpdating = True
End Sub

I placed this in module, ran marco, but nothing happened, maybe im missing something
i appreciate your help
 

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
346
Office Version
  1. 2019
Platform
  1. Windows
...You'll need to place the code in the worksheet module, pertinent to the sheet in question.
Either that, or reference the full sheet name in question, thus:
VBA Code:
Sub fnd_rep()
Dim cl As Range

Application.ScreenUpdating = False

    On Error Resume Next

    For Each cl In Sheets("Sheet1").UsedRange.SpecialCells(xlFormulas)
        cl.Formula = Replace(cl.Formula, "77777", "177777")
    Next cl

Application.ScreenUpdating = True
End Sub
... replace "Sheet1" with the sheet name, in question.
Edit - sorry - missed a double quote off "Sheet1." Corrected now.

Thanks have ran macro, hopefully will work, not sure how long it will take as there is alot of data
thanks again
 

Watch MrExcel Video

Forum statistics

Threads
1,123,318
Messages
5,600,923
Members
414,416
Latest member
Nobu

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
Top