excel - 工作表更改两个选择相交

标签 excel vba

我尝试在 Worksheet_Change 中对 Intersect 使用两种选择,但下面的代码不起作用。当我运行宏时没有任何反应,即使我使用 Not Intersect Is Nothing,它仍然不起作用。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub

    If Intersect(Target, Range("A3:A100")) Then
        MsgBox "column A" & Target.Value
    End If

    If Intersect(Target, Range("B3:B100")) Then
        MsgBox "column B" & Target.Value
    End If
End Sub

最佳答案

1.If Target.Cells.Count > 1 then Exit Sub 更改为 If Target.Cells.CountLarge > 1 then Exit SubExplanation

2.If Intersect(Target, Range("A3:A100")) then 更改为 If Not Intersect(Target, Range("A3: A100")) 那什么都不是

3.删除第一个End If

4.If Intersect(Target, Range("B3:B100")) then 更改为 ElseIf Not Intersect(Target, Range("B3: B100")) 那什么都不是

5. 您可能需要在列名称后添加空格或分隔符,否则结果将与列字母连接起来。例如,将 MsgBox "column A"& Target.Value 更改为 MsgBox "Column A : "& Target.Value

所以你的代码现在变成了

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge > 1 Then Exit Sub

    If Not Intersect(Target, Range("A3:A100")) Is Nothing Then
        MsgBox "Column A : " & Target.Value
    ElseIf Not Intersect(Target, Range("B3:B100")) Is Nothing Then
        MsgBox "Column B : " & Target.Value
    End If
End Sub

关于excel - 工作表更改两个选择相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74713136/

相关文章:

java - Excel/Java/Json 的编码问题(德语元音变音)

vba - 如果多个条件匹配,则返回 true

Excel VBA 错误从控制表单框中调用 Sub

excel - 执行宏后 Excel 单元格丢失格式,如何修复单元格格式

image - Excel 2016 vba 将图片插入并调整大小到范围

excel - 遍历已填充的行

excel - 使用单元格值作为变量在 Excel 中插入方程(方程编辑器)

xml - xml中excel vba宏的位置

excel - 查找团队中第二大的分数并返回名称

vba - Application.OnKey 无法正确重置