Excel公式设置单元格值

时间:2013-01-20 13:56:40

标签: excel set excel-formula

我的Excel电子表格中有两列我希望互相排斥。 例如 B1公式:IF(A1包含“是”,B1则设为“ - ”) A1公式:IF(B1包含“是”,A1则设为“ - ”)

基本上这应该意味着当一个单元格更改为“是”时,其旁边的另一个单元格会自动更改为“ - ”

当我尝试在excel公式构建器中执行此操作时,它表示我已创建循环公式。有谁知道我能做些什么来使这项工作?

由于

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样:

' On cell A1
=IF(B1="Yes","-","Yes")

' On cell B1
=IF(A1="Yes","-","Yes")

缺点是在A1被赋予“是”或“ - ”值之后,它不再具有该值作为其值。这可能不是问题 - 只需将B1保留为功能,并根据需要设置A1(B1将更新,因此您无需更新)

不完全是你的要求,但希望有一天能帮助别人!

答案 1 :(得分:0)

这只能在VBA中实现。将以下代码复制到工作表的模块中(例如Sheet1):

Private mBlnIsRunning As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)

    If Application.Intersect(Target, Me.Range("A1:B1")) _
        Is Nothing Then Exit Sub

    'Prevent endless loop
    If mBlnIsRunning Then Exit Sub

    mBlnIsRunning = True

    If Target.Address(False, False) = "A1" Then
        Me.Range("B1") = Negative(Target.Value)
    Else
        Me.Range("A1") = Negative(Target.Value)
    End If

    mBlnIsRunning = False
End Sub

Private Function Negative(varValue As Variant) As Variant
    'Change this function according to your needs
    If varValue = "yes" Then
        Negative = "-"
    ElseIf varValue = "-" Then
        Negative = "yes"
    Else
        Negative = "Please enter either yes or -!"
    End If
End Function