Modify Formula with Macro

Alphacsulb

Active Member
Joined
Mar 20, 2008
Messages
414
I have some formulas in cells that I need to change.

What I need is a quick way to change the formulas.

This is my current data:


As you can see the pattern is that the cell below is 3 more than the previous one.

What I have been doing manually is changing each one...which takes a long time. For instance:

B2017 now needs to start with 1562 instead of 1484...therefore B2018 should now be 1565 instead of 1487. B2019 should now be 1568 instead of B1490....ect.

Id like to be able to make this macro run 25 times since that is the number of cells.

Any leads? I have a lot of these to do. :(

<table style="font-family: Arial; font-size: 9pt;" border="1" cellpadding="2" cellspacing="0"></table>
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi

When I looked at this I thought it should be easy, but it's actually a bit fiddly (or maybe I've just made it complicated). Anyway, this macro might work for you - give it a try and see if it helps.

Code:
Sub ChangeFormulas()
Dim shtTarget As Worksheet
Dim lStartRow As Long, lEndRow As Long, lRowLoop As Long, lColumn As Long
Dim lNumEndPos As Long, lNumStartPos As Long
Dim lOffset As Long, sFormula As String, lCurrentRowRef As Long

Set shtTarget = ActiveSheet 'change as necessary - you can put a loop in if you have multiple worksheets


lColumn = 2 'assumes column B
lStartRow = 2017 'assumes that the formulas start in row 2017
lEndRow = 2041 'assumes that the formulas finish in row 2041
lOffset = (1562 - 1484) 'how many rows do we want to add (or subtract) from the original row reference

For lRowLoop = lStartRow To lEndRow

    'store the formula string
    sFormula = shtTarget.Cells(lRowLoop, lColumn).Formula
        
    'now find where the numeric part of the formula starts and finishes
    lNumEndPos = InStrRev(sFormula, "&")
    lNumStartPos = InStrRev(sFormula, "$") + 1
    
    'this will be the currently referenced row in the formula
    lCurrentRowRef = CLng(Mid$(sFormula, lNumStartPos, lNumEndPos - lNumStartPos))
        
    'Reconstruct formula adding in the offset value to the current row referenced
    sFormula = Left$(sFormula, lNumStartPos - 1) & (lCurrentRowRef + lOffset) & Right$(sFormula, Len(sFormula) - lNumEndPos + 1)

    'and finally stick the formula back into the cell
    shtTarget.Cells(lRowLoop, lColumn).Formula = sFormula
    

Next lRowLoop

End Sub

The code is currently a little hard coded. If you have multiple workbooks with different ranges then it would not be too hard to make it more dynamic. Let us know how you get on and we'll go from them.



DK
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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