EXCEL VBA 检查是否只有用户单击 A 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16891805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:25:11  来源:igfitidea点击:

EXCEL VBA Check if only user click column A

excelvba

提问by 4 Leave Cover

how do I check if the user click on the column A? I tried using:

我如何检查用户是否点击了 A 列?我尝试使用:

if selection = worksheets(Sheet1).range("A:A") then 
   *do something* 
else exit sub
end sub

will not work. Please guide me. Thank you.

不管用。请指导我。谢谢你。

采纳答案by Kazimierz Jawor

If you need to check in which column is currently selected range you could do it in two ways:

如果您需要检查当前选择的范围是哪一列,您可以通过两种方式进行:

  1. in situation when you refer to one single cell we could say that Activecellequals to Selection. Therefore you could use this code:

    If Activecell.Column = 1 then
        *do something*
    else 
        exit sub
    end sub
    
  2. it could happened that your selection range is bigger than one single cell. If you want to check if part of it (left column) is within Column A you could use this code:

    If Selection.Columns(1).Column = 1 then
        *do something*
    else 
        exit sub
    end sub
    
  1. 在您引用单个单元格的情况下,我们可以说它Activecell等于Selection。因此,您可以使用此代码:

    If Activecell.Column = 1 then
        *do something*
    else 
        exit sub
    end sub
    
  2. 您的选择范围可能大于一个单元格。如果您想检查它的一部分(左列)是否在 A 列内,您可以使用以下代码:

    If Selection.Columns(1).Column = 1 then
        *do something*
    else 
        exit sub
    end sub
    

EDITafter additional questions from comments below:

在以下评论中提出其他问题后进行编辑

To include all three conditions into one check we would go this way (first option from above is good enough):

要将所有三个条件都包含在一个检查中,我们将采用这种方式(上面的第一个选项就足够了):

    If Activecell.Column = 1 And Selection.Cells.Count = 1 And Activecell.Offset(0,1) <> "" Then
        *do something*
    else 
        exit sub
    end sub

回答by Andrey Gordeev

Use Target.Column:

使用Target.Column

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1

    End If
End Sub