简体   繁体   中英

Excel VBA - Write Formula referencing cell values

I am trying to enter a formula in a cell referencing cells which will be filled manually. For example, A3 references A1 and A2 , but those two values will be filled by me.

In this case, cell [dateColumnCounter][r] = [dateColumnCounter][r+!] - [dateColumnCounter][r+2]

I can't use direct cell names unfortunately because those will change based on the counter values.

This is what I thought would work, but doesn't:

Worksheets("Hours").Cells(r, dateColumnCounter).Formula = "=" & 
Worksheets("Hours").Cells(r+1, dateColumnCounter).Value & "-" & 
Worksheets("Hours").Cells(r+2, dateColumnCounter).Value

Try to do something equivalent as:

Sub Sum()

    With Sheets("Sheet1")
        .Cells(3, 1).Formula = "=SUM(" & .Cells(1, 1).Value & "+" & .Cells(2, 1).Value & ")"
    End With

End Sub

This particular code worked for me. Pay attention to the Sheet name and the cells you need to use.

I know you need to turn the Row and Column numbers into iterable indexes, but that's easy from here.

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