Excel VBA查找用户所选范围的列号(通过鼠标)

时间:2015-05-05 04:48:48

标签: excel vba excel-vba

我有一个允许用户用鼠标选择列的脚本。然后,用户选择该列是否具有标题。如何定义所选范围的列号,以便我可以在其上执行以下功能:

usc = rng.columns

For i = 2 to lastrow

cells(i,usc + 1).Value = left(Cells(i,usc),2)

next i

我的脚本如下。任何帮助将不胜感激!

    Set rng = Application.InputBox( _
                Prompt:="Please select your target column. "     & vbNewLine & _
                        " (e.g. Column A or Column B)", _
                Title:="Select Column", Type:=8)
    On Error GoTo 0
    hdr = MsgBox("Does your selection contain a header?", vbYesNo +     vbQuestion, "Header Option")

1 个答案:

答案 0 :(得分:1)

试试这个

Sub test()
Set Rng = Application.InputBox("Please select your target column. " & Chr(10) & _
                        " (e.g. Column A or Column B)", "Select Column", , , , , , 8)

MsgBox "Selected column is: " & Rng.Column & Chr(10) & "Selected row is: " & Rng.Row

End Sub

结果

enter image description here