Board logo

標題: [發問] 選單判斷 [打印本頁]

作者: mark761222    時間: 2015-10-7 03:50     標題: 選單判斷

透過下面語法建立選單
Private Sub UserForm_Activate()
ComboBox1.AddItem "資料1"
ComboBox1.AddItem "資料2"
ComboBox1.AddItem "資料3"

ComboBox2.AddItem "A"
ComboBox2.AddItem "B"
ComboBox2.AddItem "C"
ComboBox2.AddItem "D"
ComboBox2.AddItem "E"
ComboBox2.AddItem "F"

如何判斷 當我選資料1的時候ComboBox2.AddItem 只顯示AB
當我選資料2的時候ComboBox2.AddItem 只顯示CDE
當我選資料3的時候ComboBox2.AddItem 只顯示F

IF ComboBox1.Text = "資料1" Then           後面就不會寫了

有大大可以解答嗎?拜託了
作者: mark761222    時間: 2015-10-7 12:59

睡了一覺,自己解決了,分享給大家
Private Sub UserForm_Activate()
ComboBox1.AddItem "資料1"
ComboBox1.AddItem "資料2"
ComboBox1.AddItem "資料3"

Private Sub ComboBox1_Change()  
If ComboBox1.Text = "資料1" Then ComboBox2.clear  '選定資料後清除,避免選兩次會纍加清單
If ComboBox1.Text = "資料1" Then ComboBox2.AddItem "A"
If ComboBox1.Text = "資料1" Then ComboBox2.AddItem "B"
If ComboBox1.Text = "資料2" Then ComboBox2.clear
If ComboBox1.Text = "資料2" Then ComboBox2.AddItem "C"
If ComboBox1.Text = "資料2" Then ComboBox2.AddItem "D"
If ComboBox1.Text = "資料2" Then ComboBox2.AddItem "E"
If ComboBox1.Text = "資料3" Then ComboBox2.clear
If ComboBox1.Text = "資料3" Then ComboBox2.AddItem "F"
作者: GBKEE    時間: 2015-10-7 14:17

回復 2# mark761222
可簡潔一下,試試看
  1. Private Sub ComboBox1_Change()
  2.     With ComboBox2
  3.         .Clear
  4.         If ComboBox1.Text = "資料1" Then .List = Array("A", "B")
  5.         If ComboBox1.Text = "資料2" Then .List = Array("C", "D", "E")
  6.         If ComboBox1.Text = "資料3" Then .List = Array("F")
  7.     End With
  8. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)