2016-11-30 119 views
0

我正在Excel VBA上編寫腳本UserForm。該表格有三個不同的字段,即ComboBox由用戶填寫。我想要使​​ComboBox 3的內容取決於在Combobox2中輸入的內容。如何凍結ComboBox Excel VBA

ComboBox2填充如下:

With ComboBox2 
     .AddItem "Legal Information" 
     .AddItem "Media" 
     .AddItem "Official Disclosures" 
     .AddItem "Patents and Trademarks" 
     .AddItem "Private Corporate Information" 
     .AddItem "Private Individual Information" 
     .AddItem "Property Information" 
     .AddItem "Public Company Information" 
     .AddItem "Public Tenders" 
     .AddItem "Ships, Vessels and Aircraft Information" 
     .AddItem "Watchlists/Blacklists" 
    End With 

根據什麼ComboBox2用戶輸入,ComboBox3是由不同的選擇填充。我做的,例如如下:

Private Sub ComboBox2_Change() 

Dim index As Integer 
index = ComboBox2.ListIndex 

ComboBox3.Clear 

Select Case index 
    Case Is = 0 
     With ComboBox3 
      .AddItem "Administrative" 
      .AddItem "Civil" 
      .AddItem "Criminal" 
     End With 
    Case Is = 1 
     With ComboBox3 
      .AddItem "Arts and Culture" 
      .AddItem "Blog/Social Media" 
      .AddItem "Business and Economics" 
      .AddItem "General News" 
      .AddItem "Intelligence and Security" 
      .AddItem "Official News Agency/Official Press" 
      .AddItem "Energy" 
      .AddItem "Pharmaceutical and Medical News" 
      .AddItem "Politics" 
      .AddItem "Religion" 
      .AddItem "Society, Lifestyle and Opinion" 
      .AddItem "Sport" 
     End With 

End Sub 

我想爲ComboBox3被凍結,也就是說,不可能爲用戶填寫,在任何其他的ComboBox2選項被選中的情況下 - 在事件Case is = 2, 3, 4, 5, 6, 7, 8, 9, 10 。我應該如何做到這一點。謝謝。

+4

'ComboBox3.Enabled = False'? – CMArg

+2

使用鎖定或鎖定使用.enabled –

+2

FWIW'Case Is = 0'可以更簡單地寫爲'Case 0'。 –

回答

0

正如評論中所述,ComboBox3ComboBox3.Enabled = False「凍結」。但是,爲了避免用戶「凍結」ComboBox然後選擇ComboBox2中的其他選項,必須在每個索引情況前插入ComboBox3.Enabled = True

更正後的代碼如下。

Private Sub ComboBox2_Change() 

Dim index As Integer 
index = ComboBox2.ListIndex 

ComboBox3.Clear 

Select Case index 
    Case Is = 0 
     ComboBox3.Enabled = True 
     With ComboBox3 
      .AddItem "Administrative" 
      .AddItem "Civil" 
      .AddItem "Criminal" 
     End With 
    Case Is = 1 
     ComboBox3.Enabled = True 
     With ComboBox3 
      .AddItem "Arts and Culture" 
      .AddItem "Blog/Social Media" 
      .AddItem "Business and Economics" 
      .AddItem "General News" 
      .AddItem "Intelligence and Security" 
      .AddItem "Official News Agency/Official Press" 
      .AddItem "Energy" 
      .AddItem "Pharmaceutical and Medical News" 
      .AddItem "Politics" 
      .AddItem "Religion" 
      .AddItem "Society, Lifestyle and Opinion" 
      .AddItem "Sport" 
     End With 
    Case Is = 2 
     ComboBox3.Enabled = False 
    Case Is = 3 
     ComboBox3.Enabled = False 
    Case Is = 4 
     ComboBox3.Enabled = False 
    Case Is = 5 
     ComboBox3.Enabled = False 
    Case Is = 6 
     ComboBox3.Enabled = False 
    Case Is = 7 
     ComboBox3.Enabled = False 
    Case Is = 8 
     ComboBox3.Enabled = False 
    Case Is = 9 
     ComboBox3.Enabled = False 
    Case Is = 10 
     ComboBox3.Enabled = False 
End Select 

End Sub 
+0

您可以在'ComboBox3.Clear'之後添加'ComboBox3.Enabled = False',開始禁用該cmb。而不是列出所有其他案例(2,3,4等),只需編寫'Case Else' – CMArg

1

使用組合框的「MatchRequired」,將其設置爲TRUE,然後用戶可以選擇或僅鍵入組合框中存在的項目。無需凍結它。