Excel 2010 VBA - 无法将范围从标准模块传递到工作表模块

时间:2015-02-11 03:49:39

标签: excel vba excel-vba

我有一个click事件子,它触发一个确定范围的函数。我希望将该范围传递回click事件子,以便我可以使用.ListFillRange将结果推送到ListBox。

我在结束函数行中遇到类型不匹配错误。

对此的任何想法都表示赞赏。

Private Sub ComboBox1_Click()

    With Sheets("Search").ListBox1
        .ListFillRange = SingleTransDetail(ComboBox1.Value) '.Address(external:=True) 'TRANSACTION_DETAIL
        .Object.ColumnHeads = True
        .Object.IntegralHeight = False
        .Object.Font.Size = 11
    End With

End Sub

Function SingleTransDetail(ClickValue) As Range

    Dim COLUMN_NUMBER As Long
    Dim COLUMN_LETTER As String
    Dim ROW_POSITION As Long
    Dim RANKNUMBER As Long

    COLUMN_NUMBER = Sheets("InvoiceData_TransIDSort").Cells.Find("Transaction ID", Sheets("InvoiceData_TransIDSort").Range("A1"), xlFormulas, xlWhole, xlByColumns, xlNext).Column
    COLUMN_LETTER = Split(Evaluate("address(1," & COLUMN_NUMBER & ")"), "$")(1)

    'Give a name to the range
    Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & "2", Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & Rows.Count).End(xlUp)).Name = "TransactionIDList"

    'Find the Rank number to link to the correct line on the InvoiceData tab
    ROW_POSITION = Application.Match(ClickValue, Sheets("InvoiceData_TransIDSort").Range("TransactionIDList"), 0)
    Debug.Print ROW_POSITION
    RANKNUMBER = Sheets("InvoiceData_TransIDSort").Cells(ROW_POSITION + 1, 1).Value
    Debug.Print RANKNUMBER


    With Sheets("InvoiceData")
        COLUMN_NUMBER = .Cells.Find("*", .Range("A1"), xlFormulas, xlPart, xlByColumns, xlPrevious).Column
        Set SingleTransDetail = .Range(.Cells(RANKNUMBER + 1, 1), .Cells(RANKNUMBER + 1, COLUMN_NUMBER))
    End With

End Function

更新 我已经更新了代码,所以它现在运行并且SHEET_WITH_RANGE变量具有正确的字符串,即: “表(” InvoiceData “)范围。(” $ A $ 1867年:$ $ AG 1867年 “)”。

还有人向我建议,Columns属性分配的列数可能会导致问题,所以现在我计算cols的数量并用它显式设置ListBox。

但我在ListBox中仍然没有得到任何东西。

我们非常感谢您在哪里寻找识别问题的想法。

Private Sub ComboBox1_Click()

    COL_RANGE = SingleTransDetail(ComboBox1.Value).Address
    SHEET_WITH_RANGE = "Sheets(""InvoiceData"").Range(""" & COL_RANGE & """)(external:=True)"
    NUM_COLS = Range(COL_RANGE).Columns.Count

    With Sheets("Search").ListBox1
        .ListFillRange = SHEET_WITH_RANGE   
        .Object.ColumnHeads = True
        .Object.ColumnCount = NUM_COLS
        .Object.IntegralHeight = False
        .Object.Font.Size = 11
    End With

End Sub


Function SingleTransDetail(ClickValue) As Range

    Dim COLUMN_NUMBER As Long
    Dim COLUMN_LETTER As String
    Dim ROW_POSITION As Long
    Dim RANKNUMBER As Long

    COLUMN_NUMBER = Sheets("InvoiceData_TransIDSort").Cells.Find("Transaction ID", Sheets("InvoiceData_TransIDSort").Range("A1"), xlFormulas, xlWhole, xlByColumns, xlNext).Column
    COLUMN_LETTER = Split(Evaluate("address(1," & COLUMN_NUMBER & ")"), "$")(1)

    'Give a name to the range
    Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & "2", Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & Rows.Count).End(xlUp)).Name = "TransactionIDList"

    'Find the Rank number to link to the correct line on the InvoiceData tab
    ROW_POSITION = Application.Match(ClickValue, Sheets("InvoiceData_TransIDSort").Range("TransactionIDList"), 0)
    Debug.Print ROW_POSITION
    RANKNUMBER = Sheets("InvoiceData_TransIDSort").Cells(ROW_POSITION + 1, 1).Value
    Debug.Print RANKNUMBER


    With Sheets("InvoiceData")
        COLUMN_NUMBER = .Cells.Find("*", .Range("A1"), xlFormulas, xlPart, xlByColumns, xlPrevious).Column
        Set SingleTransDetail = .Range(.Cells(RANKNUMBER + 1, 1), .Cells(RANKNUMBER + 1, COLUMN_NUMBER))
    End With

End Function

更新2 在完成这个过程中,我将Function更改为Sub并将所有ListBox命令推送到Sub。关键是将范围的字符串rep设置为范围对象(我在此处找到了解决方案:VBA Range from String)然后将该范围对象分配给ListFillRange属性。它应该可以将ListFillRange放回到click子目录中,但是现在它已经足够了。

Private Sub ComboBox1_Click()

    Call SingleTransDetail(ComboBox1.Value)

End Sub



Sub SingleTransDetail(ClickValue)

    Dim COLUMN_NUMBER As Long
    Dim COLUMN_LETTER As String
    Dim ROW_POSITION As Long
    Dim RANKNUMBER As Long
    Dim SET_OF_CELLS As String
    Dim SHEET_WITH_RANGE As Range
    Dim NUM_COLS As Range

    COLUMN_NUMBER = Sheets("InvoiceData_TransIDSort").Cells.Find("Transaction ID", Sheets("InvoiceData_TransIDSort").Range("A1"), xlFormulas, xlWhole, xlByColumns, xlNext).Column
    COLUMN_LETTER = Split(Evaluate("address(1," & COLUMN_NUMBER & ")"), "$")(1)

    'Give a name to the range
    Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & "2", Sheets("InvoiceData_TransIDSort").Range(COLUMN_LETTER & Rows.Count).End(xlUp)).Name = "TransactionIDList"

    'Find the Rank number to link to the correct line on the InvoiceData tab
    ROW_POSITION = Application.Match(ClickValue, Sheets("InvoiceData_TransIDSort").Range("TransactionIDList"), 0)
    Debug.Print ROW_POSITION
    RANKNUMBER = Sheets("InvoiceData_TransIDSort").Cells(ROW_POSITION + 1, 1).Value
   Debug.Print RANKNUMBER

    With Sheets("InvoiceData")
        COLUMN_NUMBER = .Cells.Find("*", .Range("A1"), xlFormulas, xlPart, xlByColumns, xlPrevious).Column

        SET_OF_CELLS = .Range(.Cells(RANKNUMBER + 1, 1), .Cells(RANKNUMBER + 1, COLUMN_NUMBER)).Address
    End With

    Set SHEET_WITH_RANGE = Sheets("InvoiceData").Range(SET_OF_CELLS)

    With Sheets("Search").ListBox1
        .ListFillRange = SHEET_WITH_RANGE.Address(external:=True)                      
        .Object.ColumnHeads = True
        .Object.ColumnCount = COLUMN_NUMBER
        .Object.IntegralHeight = False
        .Object.Font.Size = 11
    End With

End Sub

0 个答案:

没有答案