从InputBox中获取用户选择的列字母输出

时间:2017-07-20 15:34:22

标签: excel excel-vba vba

我的目标是在单元格中输出一个列字母。

此处要求用户单击一个单元格(此单元格范围地址将是我想要的字母)。然后调用另一个sub,它从用户选择中获取列字母并将其写入单元格。

Dim picker As Range

Set picker = Application.InputBox("Column for total number of bar", "Choose column", Type:=8)

Call WriteColFromPicker(picker, "H19")

这是被叫子:

Sub WriteColFromPicker(pickedRng As Range, targetCell As String)
'Writes the column letter to Form controls - Used in column letter picker

Dim ws As Worksheet: Set ws = Worksheets("Form Controls")

Debug.Print targetCell

Dim chosen As String: chosen = Split(pickedRng.Address, "$")(1)
            ws.Range(targetCell).Value = chosen

End Sub

但是,没有任何内容写入单元格。

有趣的是,你会看到我有Debug.Print没有返回任何东西......

可能在这里遗漏了一些简单的东西,但我看不到它

另一个注意事项,当我没有将它放在单独的子中时,它工作正常,我将字母提取和写入单元格分成另一个子,因为我打算多次调用它。

由于

1 个答案:

答案 0 :(得分:0)

我在空工作簿中尝试此代码,它可以正常工作,打印所选单元格的列:

Option Explicit

Public Sub TestMe()

    Dim picker As Range    
    Set picker = Application.InputBox("Column for total number of bar", "Choose column", Type:=8)
    Call WriteColFromPicker(picker)

End Sub

Sub WriteColFromPicker(pickedRng As Range)

    Dim ws As Worksheet: Set ws = Worksheets(1)
    Dim chosen As String: chosen = Split(pickedRng.Address, "$")(1)
    Debug.Print chosen

End Sub