VBA ListBox.Selected始终返回false

时间:2017-09-06 01:37:12

标签: excel vba excel-vba

我正在尝试创建单击按钮时显示的ListBox。并从数组中获取列表数据。但由于某种原因,即使我点击了ListBox,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

我也会查看其他人的代码以及使用所选功能。有人可以帮帮我,如何解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

请尝试下面的代码

<强>代码

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

在Workbook open上添加以下内容

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