2017-09-06 203 views
0

我試圖創建列表框,單擊按鈕時顯示。並從數組中獲取列表數據。但由於某種原因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 

我看其他人的代碼及其使用選定的功能。有人可以幫助我,如何解決這個問題。謝謝。

+0

我不能複製它顯示'FALSE'但是,忽略了那一刻,你想用那個'If'做什麼?這似乎是說,如果選擇了某個東西,那麼就是將'xSelLst'設置爲字符串''data11; data10; data9; ... data2; data1;「'。這似乎是一件很奇怪的事情。你對**不感興趣嗎?**選擇了什麼**,而不是僅僅選擇** **? – YowE3K

+0

xLstBox.Selected(xLstBox.ListIndex)to xLstBox.Selected(I) –

+0

@ YowE3K謝謝您的回覆。我正在嘗試獲取所選內容並將其打印出來。你有另一種方式來做到這一點? –

回答

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 

上打開工作簿添加以下

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