Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

How do I replace a formula to stay on a specific cell in vba when the sheet it references updates

$
0
0

I've been playing with this code for the past week and have gotten it to 90% of what I want it to do.

Rows 14 and 15 were manually selected when it comes to the formulas and are referencing subtotal cells on the Media Plan sheet. These are correct.

This is the code in a nutshell: "Insert new vendor" will copy from "ref sheet" defined cells "New_Vendor" it is 19 rows including subtotal row with formulas. It then creates a new row in "P & L sheet" with the same formatting from the above cell. It will then insert into the new row formulas that reference the cells from subtotal row in "Media Plan sheet"

This all works great the first time.


The issue is when you run the macro a second time. Because I used offset from a defined name it updates the Vendor row to read the wrong line.
How do I stop this?
Is there a way to have the code run multiple times with each iteration referencing the correct new rows?




In Image 1 Rows 16 and 17 were created using the Macro code

Image shows formulas

Name Manager with Defined Names

Any help would be much appreciated.

Thank you.

Sub Insert_New_VendorPNL()
'' Insert_New_Vendor Macro
''Screen won't update
    Application.ScreenUpdating = False

'Select from hidden Ref sheet rows 13:23 copy and paste to
'Media Plan sheet above Grand Total Row

    Sheets("Ref").Select
    Application.Goto Reference:="New_Vendor"
    Application.CutCopyMode = False
    Selection.Copy
'Go to Media Plan Sheet, Select Grand Total Row insert new rows
    Sheets("Media Plan").Select
    Application.Goto Reference:="Grand_Total_Row"
    Selection.Insert Shift:=xlDown
'Go to P&L sheet insert new row above Total
    Application.Goto Reference:="PnL_Total_Row"
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

'Insert formulas
'A Insert Publisher Name
    Application.Goto Reference:="PnLTotalA"'select PnLTotalA Cell
    ActiveCell.Offset(-1, 0).Select                             'Go one row up
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_Publisher,-1,0)"'Insert Formula =OFFSET("Named Reference, 1 row up, 0 column change)

'C Client Gross (revenue)
    ActiveCell.Offset(0, 2).Select                              'Select cell to same row 2 columns to the right
    ActiveCell.FormulaR1C1 = "=RC[1]/0.85"'Inserts Formula =R (Row Null)C[1 Column to the right]/0.85)
'D Client Net (Revenue)
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ClientNet,-1,0)"'E Buyer Gross Budget Assigned
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[1]/0.85"'F Buyer Net Budget Assigned
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ETNegotiatedNet,-1,0)"'G Station Gross Purchased
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-2]"'H Cash % Purchased
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ETPercentCash,-1,0)"'I Trade % Purchased
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ETPercentTrade,-1,0)"'J Net Cash Cost
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ETCashCost,-1,0)"'K Gross Trade Amount
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[1]/0.85"'L Net Trade Cost
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=OFFSET(MP_GT_ETTotalTrade,-1,0)*OFFSET(MP_GT_ETTradeFactor,-1,0)"'M Net ET Cost
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-3]+RC[-1]"'N Net Media Spread
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-10]-RC[-1]"'O Media Spread Index
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-11]"'P Est. Trade Credit Usage
'Q *Trade Credit Usage
    ActiveCell.Offset(0, 2).Select
    ActiveCell.FormulaR1C1 = "=RC[-1]*RC[-13]"'R Estimated ERT Net Profit
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-4]-RC[-1]"'S ERT Net Profit %
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-15]"'T Gross Media Margin
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/(RC[-16]-RC[-3])"'go to and select Grand Total Row
'    Application.Goto Reference:="Grand_Total_Row"


End Sub```

Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>