【发布时间】:2017-05-17 16:07:13
【问题描述】:
这是我的困境:
我遵循了 Ron de Bruin 的添加新行/单元格公式,效果非常好;但是,我正在获取在新单元格中输入的值并在计算中使用它们......这也在添加的新单元格中。我将在下面引用我引用的代码:
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>>
所以,基本上我想知道的是: 1. 如何将公式添加到使用两个新创建的单元格的值的单元格(第 3 列和第 4 列中的新单元格)?
基本布局:
第 16 行,A 列(预设文本输入)| B 列(InputBox 生成单元格值)| C 列(InputBox 生成单元格值)| D列(计算B * C的公式)| E列(计算D * B的公式)
我可以通过 VBA 得到公式来计算,这不是问题;我的问题是,如果用户更改 B/C 列中任何新创建的单元格中的值,我如何/可以更新 D 列和 E 列中新创建的单元格中的公式?
我正在考虑使用 Worksheet.Calculate,但我不确定它是否适用于 VBA 计算。
如果需要,我可以发布完整的 Sub() 以查看。
【问题讨论】:
-
有几个选项。您可以使用VBA编写
Cell.Formula = "= B3 * C3"之类的公式,也可以将计算函数标记为volatile。 -
我认为我没有正确阅读此内容...您在 D/E 中进行了动态计算,即 formula="=B#*C#"?如果是这种情况,我看不出问题,因为这将采用 C/B 中使用的任何值。如果我理解错了,那么为什么不将 D/E 填写为动态的呢?我不知道您是否能够将计算填写到最后一行,或者您是否只希望特定行具有此等式。
-
感谢您的好评!我会尽力回答:@destination-data:我使用了以下内容(我认为这与您的建议相似,我今晚回家时会尝试)'rng.Parent。 Cells(LastRow + 1, LastCol + 2).formula = "= ..." ' 结果是公式的“文字”副本添加到每一行和单元格。这意味着返回到单元格的值对于第 16 行(这是正确的)是相同的......然后是 17、18、19 等。它没有“更新”正确行源的公式。
-
@Cyril - 我认为你正确地跟随。我曾尝试使用 'rng.Parent.Cells(LastRow + 1, LastCol + 1).formula = "=$B$13 * $C$13" ' 但是当我创建一个包含 4 个新单元格的新行时,公式仍在计算B13 * C13。它不会根据添加的“新”行和单元格进行更新。
-
我相信父母的电话已经过时了(也许是 2003 版?)并且可能是造成这个问题的原因。此外,如果计算是在行完成后进行的,那么它将始终使用原始单元格进行回复。鉴于我正在阅读的内容,听起来父调用正在提取原始单元格位置,或者您在生成新的 col/row 之后输入了公式(将其视为代码中发生的最后一件事)。不是解决方案,而是值得深思。