我在某些单元格中有一个硬编码公式,这些公式仅在按Enter键后才会更新。
我希望此宏“单击进入单元格并按Enter”以更新值,并使公式返回值。
我有两个宏,一个宏更新一个特定的单元格,另一个宏要更新一组单元格引用。由于某种原因,只有一个单元格宏有效。有人知道为什么吗?我希望可以在多个工作表和多个行/列中使用此功能
一个单元格:
Private Sub One_Cell_Click()
Range("$D$3").Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
End Sub
多个单元格
Private Sub All_Cells_Click()
Dim cellUpdates(0 To 3) As Variant
Dim cell As Variant
cellUpdates(0) = "$D$3"
cellUpdates(1) = "$D$4"
cellUpdates(2) = "$G$5"
cellUpdates(3) = "$Y$6"
For Each cell In cellUpdates
Range(cellUpdates(cell)).Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
Next cell
End Sub
答案 0 :(得分:0)
您可以尝试以下方法:
Private Sub All_Cells_Click()
Dim cell As Variant
For Each cell In ActiveSheet.Range("D3,D4,G5,Y6").Cells
With cell
.Formula = .Formula
.Calculate
End With
Next cell
End Sub
答案 1 :(得分:0)
第一部分在下面工作,但是我试图在数组中的不同行/列中包含多个单元格。第二部分将仅在被引用为数组中第一个值的列中运行。 (如果是A列,则它将仅在A列中下降,直到I = 4,而不会移动到其他列和行)。
Private Sub Workbook_Open()
Dim cellUpdates(0 To 4) As Variant
Dim i As Integer
cellUpdates(0) = "$D$2"
cellUpdates(1) = "$D$3"
cellUpdates(2) = "$D$4"
cellUpdates(3) = "$D$5"
cellUpdates(4) = "$D$6"
For i = 0 To UBound(cellUpdates)
Range(cellUpdates(cell)).Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
Next i
End Sub
不起作用:
Private Sub All_Cells_Click()
Dim cellUpdates(0 To 4) As Variant
Dim i As Integer
cellUpdates(0) = "$A$2"
cellUpdates(1) = "$B$3"
cellUpdates(2) = "$C$4"
cellUpdates(3) = "$D$5"
cellUpdates(4) = "$E$6"
For i = 0 To UBound(cellUpdates)
Range(cellUpdates(cell)).Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
Next i
End Sub