验证更新时更改单元格值

Change a cell value when validation updates

我在更改单元格值时遇到问题。基本上当验证 Range("F112") 更改并且值为 <> "Lease" 时,Range"("F108") 需要更改为 0.

到目前为止我想出了:

模块

Sub SetLeaseToZero()

Dim Settlement As Integer
Settlement = Sheets("HV.Select Pricing").Range("F108")

If Sheets("HV.Select Pricing").Range("F112") <> "Lease" Then
Settlement = 0
Else
End If

End Sub

Sheet 改变

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Me.Range("F112")) Is Nothing Then
SetLeaseToZero
End If

End Sub

提前致谢

您需要设置结算对象才能更改该值。如下所示调整您的模块代码:

Sub SetLeaseToZero()

Dim Settlement As Range
Set Settlement = Sheets("HV.Select Pricing").Range("F108")

If Sheets("HV.Select Pricing").Range("F112").Value <> "Lease" Then
    Settlement.Value = 0
End If

End Sub

请注意,.Value 调用在技术上不是必需的,但它们有助于阐明代码。