用于连接excel的vba列表框

时间:2015-12-07 09:30:15

标签: excel vba excel-vba

我在下面的行中收到错误。

ListBox1.RowSource = "Tabelle1!A2:C" & loletzte

这是我的代码:

myFileNameDir3 = Sheet3.Range("V10").Value & TextBox116.Text & ".xlsx"

Workbooks.Open fileName:=myFileNameDir3, UpdateLinks:=0
Set ws3 = Worksheets("Sheet1")

With ListBox5
        .ColumnCount = 3
        .ColumnWidths = "1cm;2cm;2cm"
        .ColumnHeads = True
    ListBox5.RowSource = ws1.Range("A1").CurrentRegion
    'oder:
    'ListBox1.RowSource = "Tabelle1!A2:C" & loletzte
End With

1 个答案:

答案 0 :(得分:0)

问题是您需要将RowSource描述为地址(如Sheet1!A1:A12)而不是Range引用:

Dim RowSrcAddress As String
myFileNameDir3 = Sheet3.Range("V10").Value & TextBox116.Text & ".xlsx"

If InStr(1, ws1.Name, " ") Then
    RowSrcAddress = "'" & ws1.Name & "'!" & .Range("A1").CurrentRegion.Address
Else
    RowSrcAddress = ws1.Name & "!" & .Range("A1").CurrentRegion.Address
End If

Workbooks.Open Filename:=myFileNameDir3, UpdateLinks:=0
Set ws3 = Worksheets("Sheet1")

With ListBox5
        .ColumnCount = 3
        .ColumnWidths = "1cm;2cm;2cm"
        .ColumnHeads = True
    ListBox5.RowSource = RowSrcAddress
    'oder:
    'ListBox1.RowSource = "Tabelle1!A2:C" & loletzte
End With