Private Sub ListBox1_Click()
Dim X As Integer
Dim ssheeet As Worksheet
Set ssheeet = ThisWorkbook.Sheets("Sheet2")
X = ListBox1.ListIndex
ListBox2.RowSource = ssheeet.Columns((X + 1) + 4).Address
End Sub
所以这里直到我没有打开sheet2,listbox2无法从sheet2.But获取数据,因为用户需要在其他工作表上。
虽然同一个用户表单的列表框1通过从同一个Sheet2 .thru中获取列表而运行良好,但是以下代码,
Private Sub UserForm_Initialize()
Dim ssheeet As Worksheet
Set ssheeet = ThisWorkbook.Sheets("Sheet2")
Me.ListBox1.RowSource = "Sheet2!C:C"
答案 0 :(得分:0)
Range.Address(External:=True)
将返回完全限定的引用。
ListBox2.RowSource = ssheeet.Columns((X + 1) + 4).Address(External:=True)
使用Application.Intersect
您可以轻松修剪Range
以仅包含列的已用部分。
Dim Target As Range
With ThisWorkbook.Sheets("Sheet2")
Set Target = Intersect(.Columns((x + 1) + 4), .UsedRange)
If Not Target Is Nothing Then
ListBox2.RowSource = Target.Address(External:=True)
End If
End With
答案 1 :(得分:0)
或者你可以像下面那样设置List属性......
Private Sub ListBox1_Click()
Dim Target As Range
Dim X As Integer
Dim ssheeet As Worksheet
Set ssheeet = ThisWorkbook.Sheets("Sheet2")
X = ListBox1.ListIndex
With ThisWorkbook.Sheets("Sheet2")
Set Target = Intersect(.Columns((X + 1) + 4), .UsedRange)
If Not Target Is Nothing Then
ListBox2.List = Target.Value
End If
End With
End Sub