VBA code to update formulas with cell references on another tab

BillPeterson

New Member
Joined
Jan 16, 2022
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Ok I have a macro which copies a tab, and then I want it to update formulas. In A1 on the freshly created tab, I have the following formula:

Excel Formula:
=Budget!S13

How can I alter this formula with VBA code to decrement the row number?

When it's done I want it to be:

Excel Formula:
=Budget!S12
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Does this do what you want?
VBA Code:
Sub BillPeterson()
Dim x, y
'Copy a tab - change sheet name to suit
Sheets("Sheet1").Copy after:=Sheets("Sheet1")
With ActiveSheet.Range("A1")
    x = .Formula
    If InStr(1, x, "Budget!S") > 0 Then
        y = Split(x, "S")(1) - 1
        If y >= 1 Then
            .Formula = "=Budget!S" & y
        End If
    End If
End With
End Sub
 
Upvote 0
Solution
Thank you! I will give that a shot. The following worked, but I'm sure there's a simpler way.

VBA Code:
    Dim strFormula As String
    Dim Leftpart As String
    Dim RowValue As Long
   
    strFormula = Range("A1").Formula
    RowValue = Range(strFormula).Row
    RowValue = RowValue - 1
    Leftpart = Left(strFormula, 9)
    strFormula = Leftpart & RowValue
    Range("A1").Formula = strFormula

I should figure out how to convert the above into a loop so it will work for multiple cells, allowing me to easily expand the range if necessary.
 
Upvote 0
Thank you! I will give that a shot. The following worked, but I'm sure there's a simpler way.

VBA Code:
    Dim strFormula As String
    Dim Leftpart As String
    Dim RowValue As Long
  
    strFormula = Range("A1").Formula
    RowValue = Range(strFormula).Row
    RowValue = RowValue - 1
    Leftpart = Left(strFormula, 9)
    strFormula = Leftpart & RowValue
    Range("A1").Formula = strFormula

I should figure out how to convert the above into a loop so it will work for multiple cells, allowing me to easily expand the range if necessary.
You are welcome- thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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