简体   繁体   English

Excel VBA - 编写引用单元格值的公式

[英]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.例如, A3引用A1A2 ,但这两个值将由我填充。

In this case, cell [dateColumnCounter][r] = [dateColumnCounter][r+!] - [dateColumnCounter][r+2]在这种情况下, 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.我知道您需要将行号和列号转换为可迭代索引,但从这里开始很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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