使用vba excel获取用户选定单元格的列

时间:2014-03-07 20:38:40

标签: excel vba excel-vba

请参阅此问题:Let the user click on the cells as their input for an Excel InputBox using VBA。 我使用这行代码Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", Title:="CELL TO EDIT", Type:=8)要求用户选择一个单元格。如果选择的单元格在G列中,那么我将需要调用子程序。所以,我需要找出所选单元格所在的列。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用rng.Column属性返回列号:

Sub test()
    Dim rng As Range
    On Error Resume Next
    Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", title:="CELL TO EDIT", Type:=8)
    On Error GoTo 0

    If rng Is Nothing Then Exit Sub

    'column G=7
    If rng.Column = 7 Then
       'do something
    End If
End Sub

如果用户可以选择多个单元格,请更改

If rng.Column = 7 Then

If Not Application.Intersect(rng, Sheets("Sheet1").Range("G:G")) Is Nothing Then