VBA ListBox.Selected始终返回false
问题描述:
我试图创建列表框,单击按钮时显示。并从数组中获取列表数据。但由于某种原因ListBox.Selected总是False,即使我点击了ListBox。VBA ListBox.Selected始终返回false
Sub Rectangle2_Click()
Dim MyList(10) As String
MyList(0) = "data1"
MyList(1) = "data2"
MyList(2) = "data3"
MyList(3) = "data4"
MyList(4) = "data5"
MyList(5) = "data6"
MyList(6) = "data7"
MyList(7) = "data8"
MyList(8) = "data9"
MyList(9) = "data10"
MyList(10) = "data11"
Dim xSelShp As Shape
Dim xSelLst As Variant
Dim I As Integer
Set xSelShp = ActiveSheet.Shapes(Application.Caller)
Set xLstBox = ActiveSheet.ListBox1
xLstBox.List = MyList ' Insert Data from array to ListBox
Set rng = ActiveSheet.Range("I10:R10") 'I must setting width,heigh and location because everytime i click the button the size become smaller and the position changed.
xLstBox.Width = 150
xLstBox.Height = 180
xLstBox.Top = rng.Top
xLstBox.Left = rng.Left
If xLstBox.Visible = False Then
xLstBox.Visible = True
xSelShp.TextFrame2.TextRange.Characters.Text = Pickup Options"
Else
xLstBox.Visible = False
xSelShp.TextFrame2.TextRange.Characters.Text = "Select Options"
For I = 0 To xLstBox.ListCount - 1
If xLstBox.Selected(xLstBox.ListIndex) Then '<< This is the problem. Always return False
xSelLst = xLstBox.List(I) & ";" & xSelLst
End If
Next I
If xSelLst <> "" Then
Range("ListBoxOutput") = Mid(xSelLst, 1, Len(xSelLst) - 1)
Else
Range("ListBoxOutput") = ""
End If
End If
End Sub
我看其他人的代码及其使用选定的功能。有人可以帮助我,如何解决这个问题。谢谢。
答
请尝试以下代码
代码
Option Explicit
Sub Rectangle2_Click()
Dim MyList(10) As String
MyList(0) = "data1"
MyList(1) = "data2"
MyList(2) = "data3"
MyList(3) = "data4"
MyList(4) = "data5"
MyList(5) = "data6"
MyList(6) = "data7"
MyList(7) = "data8"
MyList(8) = "data9"
MyList(9) = "data10"
MyList(10) = "data11"
Dim xSelShp As Shape
Dim xSelLst As Variant
Dim xLstBox As Object
Dim I As Integer
Dim rng As Range
Set xSelShp = ActiveSheet.Shapes(Application.Caller)
Set xLstBox = ListBox1
xLstBox.List = MyList ' Insert Data from array to ListBox
Set rng = ActiveSheet.Range("I10:R10") 'I must setting width,heigh and location because everytime i click the button the size become smaller and the position changed.
xLstBox.Width = 150
xLstBox.Height = 180
xLstBox.Top = rng.Top
xLstBox.Left = rng.Left
If xLstBox.Visible = False Then
xLstBox.Visible = True
xSelShp.TextFrame2.TextRange.Characters.Text = "Pickup Options"
ElseIf xLstBox.Visible = True Then
xSelShp.TextFrame2.TextRange.Characters.Text = "Pickup Options"
For I = 0 To xLstBox.ListCount - 1
If xLstBox.Selected(I) Then
xSelLst = xLstBox.List(I) & ";" & xSelLst
End If
Next I
If xSelLst <> "" Then
Range("ListBoxOutput") = Mid(xSelLst, 1, Len(xSelLst) - 1)
Else
Range("ListBoxOutput") = ""
End If
Else
xLstBox.Visible = False
xSelShp.TextFrame2.TextRange.Characters.Text = "Select Options"
End If
End Sub
上打开工作簿添加以下
Private Sub Workbook_Open()
Sheet1.Shapes.Range(Array("Rectangle 2")).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "Select Options"
Sheet1.ListBox1.Visible = False
Range("A1").Select
End Sub
我不能复制它显示'FALSE'但是,忽略了那一刻,你想用那个'If'做什么?这似乎是说,如果选择了某个东西,那么就是将'xSelLst'设置为字符串''data11; data10; data9; ... data2; data1;“'。这似乎是一件很奇怪的事情。你对**不感兴趣吗?**选择了什么**,而不是仅仅选择** **? – YowE3K
xLstBox.Selected(xLstBox.ListIndex)to xLstBox.Selected(I) –
@ YowE3K谢谢您的回复。我正在尝试获取所选内容并将其打印出来。你有另一种方式来做到这一点? –