Excel macro to update formulas in a spreadsheet

I need a macro to look at the formulas in Col C of my spreadsheet and where ever it finds a “year \month-year” reference in the formula it must change it as appropriate.  Let me explain further:

 Example 1:In cell C4 there is this formula:  =SUM('G:\INTERNAL REPORTING - POST ACQUISITION\GTCS\Executive Summary\2014\1214\[BV - 1405020 - 100.xlsx]Sheet1'!$B$21:$B$44)/1000

 When I run the macro it must change the formula to:

 =SUM('G:\INTERNAL REPORTING - POST ACQUISITION\GTCS\Executive Summary\2015\0115\[BV - 1405020 - 100.xlsx]Sheet1'!$B$21:$B$44)/1000

 Example 2:  If the macro finds this formula:
 =SUM('G:\INTERNAL REPORTING - POST ACQUISITION\GTCS\Executive Summary\2015\0115\[BV - 1405020 - 100.xlsx]Sheet1'!$B$21:$B$44)/1000

 It must change it to:

 =SUM('G:\INTERNAL REPORTING - POST ACQUISITION\GTCS\Executive Summary\2015\0215\[BV - 1405020 - 100.xlsx]Sheet1'!$B$21:$B$44)/1000

 If the formula has no date in it the macro must leave it alone.
Demo-Version.xlsx
ParaGlowAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can run the following code which will do what you are looking for...

Sub checkformulas()
    Dim rng As Range, cell As Range


    Set rng = Cells.SpecialCells(xlCellTypeFormulas)

    For Each cell In rng


        If InStr(1, cell.Formula, "\2014\1214", vbTextCompare) > 0 Then
            cell.Formula = Replace(cell.Formula, "\2014\1214", "\2015\0115")
        ElseIf InStr(1, cell.Formula, "\2015\0115", vbTextCompare) > 0 Then
            cell.Formula = Replace(cell.Formula, "\2015\0115", "\2015\0215")
        End If

    Next cell
End Sub

Open in new window


Please be cognizant of the fact that the first time you the macro it will update dates of 2014\1214 to 2015\0115..However when you the same macro again it will again change those dates from \2015\0115 to \2015\0215

Saurabh...
ParaGlowAuthor Commented:
Thanks for your help.  I will test out and let you know.
ParaGlowAuthor Commented:
I haven't yet had a chance to test your code but I was wondering whether it only handles just 2 specific situations.  Or whether it is dynamic.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Saurabh Singh TeotiaCommented:
These lines in the code which you see-->

If InStr(1, cell.Formula, "\2014\1214", vbTextCompare) > 0 Then
            cell.Formula = Replace(cell.Formula, "\2014\1214", "\2015\0115")
        ElseIf InStr(1, cell.Formula, "\2015\0115", vbTextCompare) > 0 Then
            cell.Formula = Replace(cell.Formula, "\2015\0115", "\2015\0215")

Open in new window


You can update it basis of whatever you want to replace and it will do what you are looking for..

Saurabh..
ParaGlowAuthor Commented:
ok.  That should work.  It would be better if the code could just read what's there and increment to the next month, or the next year and month, as the case might be.
Saurabh Singh TeotiaCommented:
Sure, Once you change that or update this will do what you are looking for..
ParaGlowAuthor Commented:
Not quite what I was looking for.  Each month I would have to manually make changes to the code before running it.
Saurabh Singh TeotiaCommented:
Then how do you want to define it..Can you enter the same on a cell address one of current month and another cell of defining which month you want to change to...

If you can do that..I can help you setting up to that cell..basis of that input i will take the formula from that cell...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Spreadsheets

From novice to tech pro — start learning today.