Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

update/modify formula VBA

I have multiple sheets that should have references to another sheet.
For example cell B3,sheet AC: =Inlife!G4. Now I create two new sheets AC (2) and Inlife (2), the reference for B3,sheet AC (2) should be:
=Inlife (2)!G4

I have tried many variations of the replace option but so far all that I managed to do was remove the formula and leave a value or blank cell.

  Dim rng As Range, cell As Range
    Set rng = Sheets("AC (2)").Range("B3:B10")
    

    For Each cell In rng
  cell = WorksheetFunction.Substitute(cell, "Inlife", "Inlife (2)")

    Next

Does anyone know a way to update all the references/formulas in one go?
(I have tried to just use the search and replace function of excel but that gave me an error about the formula)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

Please, try:

    For Each cell In rng
      cell.Formula = Replace(cell.Formula, "Inlife", "Inlife (2)")
    Next

I would also like to suggest replacing of cell variable with cel. Cell is a range property and it is good to avoid creating such variables. It may create confusions on a complex code…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading