自动更新单元格

huangapple go评论59阅读模式
英文:

Automatically update cells

问题

我目前在我的工作表上有这段代码:
```vba
Private Sub Worksheet_Change(ByVal Target As Range)
    ' 确保在退出此子过程时始终启用事件,即使出现错误
    On Error GoTo errExit
    Application.EnableEvents = False
    
    Dim SpotRate As Double
    SpotRate = Me.Range("H2").Value

    If Target.Address = "$C$3" Then
        If Target.Value <> "" Then
            Range("E3").Value = Target.Value * SpotRate
        Else
            Range("E3").ClearContents
        End If
    ElseIf Target.Address = "$E$3" Then
        If Target.Value <> "" Then
            Range("C3").Value = Target.Value / SpotRate
        Else
            Range("C3").ClearContents
        End If
    End If

errExit:
    Application.EnableEvents = True
End Sub

C2单元格中有一个下拉菜单,其中包含汇率信息。

当我在C3或E3中输入值时,代码按照要求工作。但是,如果我更改C2单元格中的值(这会更改H2中的汇率),这些值不会自动更新到C3或E3。有没有办法可以更改这个,使C3中的值保持不变,但根据新的汇率自动更新E3?谢谢。


<details>
<summary>英文:</summary>

I currently have this code for my worksheet:
```vba
Private Sub Worksheet_Change(ByVal Target As Range)
    &#39; ensure events are always enabled when we exit this Sub, even if we have an error
    On Error GoTo errExit
    Application.EnableEvents = False
    
    Dim SpotRate As Double
    SpotRate = Me.Range(&quot;H2&quot;).Value

    If Target.Address = &quot;$C$3&quot; Then
        If Target.Value &lt;&gt; &quot;&quot; Then
            Range(&quot;E3&quot;).Value = Target.Value * SpotRate
        Else
            Range(&quot;E3&quot;).ClearContents
        End If
    ElseIf Target.Address = &quot;$E$3&quot; Then
        If Target.Value &lt;&gt; &quot;&quot; Then
            Range(&quot;C3&quot;).Value = Target.Value / SpotRate
        Else
            Range(&quot;C3&quot;).ClearContents
        End If
    End If

errExit:
    Application.EnableEvents = True
End Sub

There is a pair a dropdown in cell C2 that contains information for the spot rate.

When I input the values into C3 or E3 the code works as required. However, if I change the value in cell C2 (which changes the spot rate in H2), these values don't automatically update in C3 or E3. Is there a way I can change this so that the value in C3 is held constant but E3 is automatically updated according to the new spot rate? Thanks.

答案1

得分: 1

尝试这个:

私有子工作表_更改(ByVal目标作为范围)
    Dim c As Range, v, SpotRate As Double
    
    如果目标.CountLarge > 1然后退出子 &#39;仅处理单元格更改
    
    如果有错误则转到 errExit
    
    SpotRate = Me.Range("H2").Value
    
    选择情况目标地址(False,False)
        案例 "C2"
            &#39;通过事件处理程序触发更新
            Me.Range("C3").Value = Me.Range("C3").Value
            Me.Range("E3").Value = Me.Range("E3").Value
        案例 "C3"
            Application.EnableEvents = False
            如果目标.Value <> "" 然后
                范围("E3").Value = 目标.Value * SpotRate
            其他
                范围("E3").ClearContents
            结束如果
        案例 "E3"
            Application.EnableEvents = False
            如果目标.Value <> "" 然后
                范围("C3").Value = 目标.Value / SpotRate
            其他
                范围("C3").ClearContents
            结束如果
    结束选择
        
errExit:
    如果错误号 <> 0然后调试打印错误说明
    Application.EnableEvents = True
结束子

请注意,我已经将代码部分保持不变,只对注释进行了中文翻译。

英文:

Try this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range, v, SpotRate As Double
    
    If Target.CountLarge &gt; 1 Then Exit Sub &#39;only handling single-cell changes
     
    On Error GoTo errExit
    
    SpotRate = Me.Range(&quot;H2&quot;).Value
    
    Select Case Target.Address(False, False)
        Case &quot;C2&quot;
            &#39;trigger updates via event handler
            Me.Range(&quot;C3&quot;).Value = Me.Range(&quot;C3&quot;).Value
            Me.Range(&quot;E3&quot;).Value = Me.Range(&quot;E3&quot;).Value
        Case &quot;C3&quot;
            Application.EnableEvents = False
            If Target.Value &lt;&gt; &quot;&quot; Then
                Range(&quot;E3&quot;).Value = Target.Value * SpotRate
            Else
                Range(&quot;E3&quot;).ClearContents
            End If
        Case &quot;E3&quot;
            Application.EnableEvents = False
            If Target.Value &lt;&gt; &quot;&quot; Then
                Range(&quot;C3&quot;).Value = Target.Value / SpotRate
            Else
                Range(&quot;C3&quot;).ClearContents
            End If
    End Select
        
errExit:
    If Err.number &lt;&gt; 0 Then Debug.Print Err.Description
    Application.EnableEvents = True
End Sub

答案2

得分: 0

如果目标地址是"$H$2",你需要一个等价的代码块来执行计算。

假设我对你尝试实现的目标理解正确,这段代码应该对你有用:

Private Sub Worksheet_Change(ByVal Target As Range)

' 确保在退出此子程序时始终启用事件,即使发生错误也是如此
On Error GoTo errExit
Application.EnableEvents = False

Dim SpotRate As Double
SpotRate = Me.Range("H2").Value

' 如果H2不是一个数字
If Not IsNumeric(SpotRate) Then
    Range("E3").ClearContents
    Range("C3").ClearContents
    Exit Sub
End If

'E3 改变
If Target.Address = "$E$3" Then
    ' 调整E3
    If IsNumeric(Range("E3").Value) Then
        Range("E3").Value = Target.Value * SpotRate
    Else
        Range("E3").ClearContents
    End If

'C3 改变
ElseIf Target.Address = "$C$3" Then
    ' 调整C3
    If IsNumeric(Me.Range("C3").Value) Then
        Range("C3").Value = Target.Value / SpotRate
    Else
        Range("C3").ClearContents
    End If

'C2 改变
ElseIf Target.Address = "$C$2" Then
    If IsNumeric(Me.Range("C2").Value) Then
        ' 调整E3
        If IsNumeric(Range("E3").Value) Then
            Range("E3").Value = Target.Value * SpotRate
        Else
            Range("E3").ClearContents
        End If
        ' 调整C3
        If IsNumeric(Me.Range("C3").Value) Then
            Range("C3").Value = Target.Value / SpotRate
        Else
            Range("C3").ClearContents
        End If
    Else
        ' C2 不是一个数字
        Range("E3").ClearContents
        Range("C3").ClearContents
    End If
End If

errExit:
    Application.EnableEvents = True
End Sub
英文:

You have blocks of code that perform some actions when the Target (range that changed, triggering the Worksheet_Change event) address is "$C$3" and "$E$3".

If Target.Address = &quot;$C$3&quot; Then

ElseIf Target.Address = &quot;$E$3&quot; Then

You need an equivalent block that will perform the calculation if Target.Address = "$H$2".

Assuming my understanding of what you're trying to achieve is correct, this should work for you:

Private Sub Worksheet_Change(ByVal Target As Range)

&#39; ensure events are always enabled when we exit this Sub, even if we have an error
On Error GoTo errExit
Application.EnableEvents = False

Dim SpotRate As Double
SpotRate = Me.Range(&quot;H2&quot;).Value

&#39;if H2 is not a number
If Not IsNumeric(SpotRate) Then
    Range(&quot;E3&quot;).ClearContents
    Range(&quot;C3&quot;).ClearContents
    Exit Sub
End If

&#39;E3 changed
If Target.Address = &quot;$E$3&quot; Then
    &#39;adjust E3
    If IsNumeric(Range(&quot;E3&quot;).Value) Then
        Range(&quot;E3&quot;).Value = Target.Value * SpotRate
    Else
        Range(&quot;E3&quot;).ClearContents
    End If
    
&#39;C3 changed
ElseIf Target.Address = &quot;$C$3&quot; Then
    &#39;adjust C3
    If IsNumeric(Me.Range(&quot;C3&quot;).Value) Then
        Range(&quot;C3&quot;).Value = Target.Value / SpotRate
    Else
        Range(&quot;C3&quot;).ClearContents
    End If
    
&#39;C2 changed
ElseIf Target.Address = &quot;$C$2&quot; Then
    If IsNumeric(Me.Range(&quot;C2&quot;).Value) Then
        &#39;adjust E3
        If IsNumeric(Range(&quot;E3&quot;).Value) Then
            Range(&quot;E3&quot;).Value = Target.Value * SpotRate
        Else
            Range(&quot;E3&quot;).ClearContents
        End If
        &#39;adjust C3
        If IsNumeric(Me.Range(&quot;C3&quot;).Value) Then
            Range(&quot;C3&quot;).Value = Target.Value / SpotRate
        Else
            Range(&quot;C3&quot;).ClearContents
        End If
    Else
        &#39;C2 is not a number
        Range(&quot;E3&quot;).ClearContents
        Range(&quot;C3&quot;).ClearContents
    End If
End If

errExit:
    Application.EnableEvents = True
End Sub

EDIT: Sorry, misuderstood the question at first, corrected my comment to include the right solution.

huangapple
  • 本文由 发表于 2023年7月6日 18:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定