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

请参阅此问题: 让用户使用VBA单击单元格作为Excelinput框的input 。 我使用这行代码Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", Title:="CELL TO EDIT", Type:=8)要求用户select一个单元格。 如果被选中的单元格在列G中,那么我将需要调用一个子例程。 所以,我需要弄清楚选中的单元格在哪一列。

谢谢您的帮助!

使用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 

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

 If rng.Column = 7 Then 

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