【问题标题】:Changing Specific Cell Condition to a Range Condition将特定单元格条件更改为范围条件
【发布时间】:2017-11-09 22:36:19
【问题描述】:

我想在G1:G500 Range 上的单元格更改(其值等于或大于“1”)时自动运行宏。

我部分成功了,但是;

  1. 它只适用于一个特定的单元格。
  2. 我无法写出哪个告诉它等于或大于 1。它仅在单元格值等于 1 时才有效。

我真的很抱歉,因为我是 VBA 的初学者。帮助表示赞赏。

请看下面的完整代码;

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("G12")) Is Nothing Then Exit Sub
If Target = "" Then Exit Sub
With Application
  .EnableEvents = False
  .ScreenUpdating = False
    If UCase(Me.Range("G12").Value) = "1" Then

      Call K999

    End If
  .ScreenUpdating = True
  .EnableEvents = True
End With
End Sub

【问题讨论】:

  • 谢谢。我只是担心是否需要加引号。
  • 它的格式实际上是百分比。我想我应该使用数字。
  • K999是做什么的?
  • 它是我的宏名:)

标签: vba excel


【解决方案1】:

试试:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G1:G500")) Is Nothing Then
    If Target = "" Then Exit Sub
    On Error GoTo GetOut
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        If Target.Value >= 1 Then
            Call K999
        End If
    End With
End If
GetOut:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

【讨论】:

  • 嗨斯科特;它工作得非常完美。我很感谢支持伙伴。
  • 很抱歉,我无法感谢你,因为我的声誉还不够。
  • 您可以单击答案旁边的复选标记,从而选择此作为正确答案。该问题必须至少存在 15 分钟,然后才能选择。
【解决方案2】:
Private Sub Worksheet_Change(ByVal Target As Range)
    '[1]
    If Intersect(Target, Range("G1:G500")) Is Nothing Then Exit Sub
    If Target = "" Then Exit Sub
    '[2]
    If Not IsNumeric(Target.Value) Then Exit Sub
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        '[3]
        If Target.Value >= 1 Then
            Call K999
        End If
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
  1. Intersect(Target, Range("G12")) 检查目标是否为 G12,根据您的说法,它应该是 Range("G1:G500")

  2. 由于不能强制用户在单元格中输入数字,因此先检查值是否为数字会更安全。

  3. “1”是字符串,1 是数字。无需转换成字符串然后比较两个字符串。

【讨论】:

  • 感谢 cmets。那时报价绝对是不必要的。我知道 G12 是单细胞。我仍在尝试使用代码了解范围详细信息。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2015-02-17
相关资源
最近更新 更多