[英]VBA to check if a cell with formula has been overwritten and refill the cell with original formula
就像標題一樣。 我有一列需要能夠手動輸入數字,或者如果沒有給出,則返回函數的值。 我知道我可以使用額外的一列,但由於文件中已經有 50 列,我不想這樣做。 所以我正在尋找的是一個 vba sub,它可以自動運行以檢查某個列中的單元格是否有手動數據,如果該數據被清除,則將原始函數放回去,基本上是一個事件觸發的 sub。
使用在 A 列上觸發的 worksheet_change 事件。如果目標為空,請輸入公式。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
On Error GoTo bye
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("A:A"))
'Debug.Print t.Value
If t.Value = vbNullString Then
t.FormulaR1C1 = "=sum(RC2:RC8)" '=SUM(B2:H2) in row 2
End If
Next t
End If
bye:
Application.EnableEvents = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.