改变特定细胞条件的范围条件
我想在G1:G500
范围的单元格更改(其值等于或大于“1”)时自动运行macros。
我部分成功,但;
- 它只适用于一个特定的单元格。
- 我不能写它告诉它等于或大于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
尝试:
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
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
-
Intersect(Target, Range("G12"))
检查目标是否为G12,根据你的说法,应该是Range(“G1:G500”) -
由于您不能强制用户在单元格中input数字,因此检查值是否为数字是比较安全的。
-
“1”是一个string,1是一个数字。 不需要转换成string然后比较两个string。