简体   繁体   中英

Updating cell formula based on added cell/row value

Here's my dilemma:

I have followed the add new row/cell formula from Ron de Bruin and it works splendidly; however, I am taking the values entered in the new cells and using them in calculations ... which are also in new cells added. I'll past the code I'm referencing below:

    rng.Parent.Cells(LastRow + 1, LastCol + 2).Value = spm
        rng.Cells(LastRow + 1, LastCol + 2).Borders(xlDiagonalDown).LineStyle = xlNone
        rng.Cells(LastRow + 1, LastCol + 2).Borders(xlDiagonalUp).LineStyle = xlNone
        With rng.Cells(LastRow + 1, LastCol + 2).Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With rng.Cells(LastRow + 1, LastCol + 2).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With rng.Cells(LastRow + 1, LastCol + 2).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With rng.Cells(LastRow + 1, LastCol + 2).Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        rng.Cells(LastRow + 1, LastCol + 2).Borders(xlInsideVertical).LineStyle = xlNone
        rng.Cells(LastRow + 1, LastCol + 2).Borders(xlInsideHorizontal).LineStyle = xlNone
            With rng.Cells(LastRow + 1, LastCol + 2)
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
    rng.Parent.Cells(LastRow + 1, LastCol + 3).Locked = False
    Range("K8").Value = tsqs
    Range("L8").Value = spm
    'Calculate value for Man Hours per Item
    rng.Parent.Cells(LastRow + 1, LastCol + 3).Formula = <<still working on this bit>>

So, basically what I am wondering is: 1. How would I add a formula to a cell (new cell in column 3 & 4) which uses the value of the two newly created cells?

Basic Layout:

Row 16, Column A (Preset Text input) | Column B (InputBox generates cell value) | Column C (InputBox generates cell value) | Column D (formula to calculate B * C) | Column E (formula to calculate D * B)

I can get formulas to calculate through VBA, that's not the issue; my issue is that if a user changes the value in any of the newly created cells in Columns B/C, how do I / can I update the formula in the newly created cells in Columns D & E?

I was thinking of using Worksheet.Calculate but I'm not certain that will work on the VBA calculations.

I can post the full Sub() to view if that is needed.

Going to try pasting some code I've got that I'm hoping will be a good example:

Private Sub ExportMath()

Dim LR As Long
    LR = Cells(Sheets("Export Report").Rows.Count, 1).End(xlUp).Row

Sheets("Export Report").Range("O2").Formula = "=NETWORKDAYS(C2,D2)"
Sheets("Export Report").Range("O2:O" & LR).FillDown 

End Sub

In this code, I define my last row only, I add a formula to be utilized, then fill it down the entire column.

If you have a defined column you will be working with (even if you insert via .Insert Shift:=xlToRight or .Insert Shift:=xlDown), you will be able to code for a specific column name. In this particular case, I apply this formula AFTER all spreadsheet manipulation has taken place; however, if I were to place this calculation in before I start inserting/deleting rows/columns, the named cells (O2:O) will be updated by Excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM