返回列表 上一主題 發帖

下拉式要如何選擇後會自動帶出其他表單訊息

回復 1# Jared
附檔說明你的問題.要解答者,好解答些!!

TOP

本帖最後由 GBKEE 於 2013-3-7 14:56 編輯

回復 3# Jared
試試看這是否如你所需
  1. 'ListIndex = -1: 所選的不在 List清單內
  2. Private Sub ComboBox1_Change()
  3.     If ComboBox1.ListIndex > -1 Then
  4.         餐點
  5.     Else
  6.         ComboBox2 = ""
  7.     End If
  8. End Sub
  9. Private Sub ComboBox2_Change()
  10.     TextBox1 = ""
  11.     If ComboBox2.ListIndex > -1 Then TextBox1 = ComboBox2.List(ComboBox2.ListIndex, 1)
  12. End Sub
  13. Private Sub CommandButton1_Click()
  14.     Dim A
  15.     If ComboBox1.ListIndex = -1 Or ComboBox2.ListIndex = -1 Then MsgBox "餐點內容 需齊全 !!": Exit Sub
  16.     With Sheet10
  17.         .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 3).Value = Array(ComboBox1, ComboBox2, TextBox1)
  18.     End With
  19. End Sub
  20. Private Sub CommandButton2_Click()
  21.     test4.Hide
  22. End Sub
  23. Private Sub CommandButton3_Click()
  24.     Me.ComboBox1 = ""
  25.     Me.ComboBox2 = ""
  26.     Me.TextBox1 = ""
  27. End Sub
  28. Private Sub UserForm_Initialize()
  29.     ComboBox1.List = Array("一號餐", "二號餐", "三號餐")
  30. End Sub
  31. Private Sub 餐點()
  32.     With ComboBox2
  33.         .Clear
  34.         Select Case ComboBox1.ListIndex
  35.             Case 0               '一號餐
  36.                 .AddItem "漢堡、薯條、可樂"
  37.                 .List(.ListCount - 1, 1) = 50           'ComboBox2的第2欄  沒顯示出來
  38.                 .AddItem "漢堡、薯餅、可樂"
  39.                 .List(.ListCount - 1, 1) = 49
  40.                 .AddItem "漢堡、薯餅、紅茶"
  41.                 .List(.ListCount - 1, 1) = 48
  42.             Case 1             '二號餐
  43.                 .AddItem "雞塊、薯條、可樂"
  44.                 .List(.ListCount - 1, 1) = 47           'ComboBox2的第2欄  沒顯示出來
  45.                 .AddItem "雞塊、薯餅、可樂"
  46.                 .List(.ListCount - 1, 1) = 46
  47.                 .AddItem "雞塊、薯餅、紅茶"
  48.                 .List(.ListCount - 1, 1) = 45
  49.             Case 2             '三號餐
  50.                 .AddItem "厚土司、薯條、可樂"
  51.                 .List(.ListCount - 1, 1) = 44           'ComboBox2的第2欄  沒顯示出來
  52.                 .AddItem "厚土司、薯餅、可樂"
  53.                 .List(.ListCount - 1, 1) = 43
  54.                 .AddItem "厚土司、薯餅、紅茶"
  55.                 .List(.ListCount - 1, 1) = 42
  56.         End Select
  57.     End With
  58.     TextBox1 = ""
  59. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2013-3-7 18:22 編輯

回復 5# Jared

附檔試試看

test.rar (25.17 KB)





  1. 'ListIndex = -1: 所選的不在 List清單內
  2. Private Sub ComboBox1_Change()
  3.     If ComboBox1.ListIndex > -1 Then
  4.         餐點
  5.     Else
  6.          ListBox1.RowSource = ""
  7.     End If
  8. End Sub
  9. Private Sub CommandButton1_Click()
  10.     Dim A
  11.     If ComboBox1.ListIndex = -1 Or Me.ListBox1.ListIndex = -1 Then MsgBox "餐點內容 需齊全 !!": Exit Sub
  12.     With Sheet10
  13.         .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 3).Value = Array(ComboBox1, ListBox1.List(ListBox1.ListIndex, 0), ListBox1.List(ListBox1.ListIndex, 1))
  14.     End With
  15. End Sub
  16. Private Sub CommandButton2_Click()
  17.     test4.Hide
  18. End Sub
  19. Private Sub CommandButton3_Click()
  20.     ComboBox1 = ""
  21. End Sub
  22. Private Sub UserForm_Initialize()
  23.     With ListBox1
  24.         .ColumnWidths = .Width * (2 / 3) & "," & .Width * (1 / 3)   '顯示欄寬度
  25.         .ColumnHeads = True                                         '欄位顯示 表頭
  26.         .TextAlign = fmTextAlignCenter                              '文字置中
  27.         .ColumnCount = 2                                            '顯示欄位數  餐點內容,價錢 兩欄
  28.        .Font.Size = 12
  29.     End With
  30.     With ComboBox1
  31.         .Font.Size = 12
  32.         .TextAlign = fmTextAlignCenter                              '文字置中
  33.     End With
  34.     套餐定義
  35. End Sub
  36. Private Sub 餐點()
  37.     With ListBox1
  38.         .RowSource = ""                                             '清除ListBox 清單的來源
  39.         .RowSource = Sheets("套餐").Names(ComboBox1.Value).RefersTo '指定ListBox 清單的來源 A1:C5 範圍的形式(不是 R1C1)
  40.     End With
  41. End Sub
  42. Private Sub 套餐定義()          '定義名稱
  43.     Dim R As Variant, i As Integer
  44.     With Sheets("套餐")
  45.         For Each R In .Names
  46.             R.Delete                '清除套餐的定義名稱
  47.         Next
  48.         Set R = .[A1]
  49.         i = 1
  50.         Do While .Cells(i, "A") <> ""
  51.             Set R = R.End(xlToRight)
  52.             ComboBox1.AddItem .Cells(i, "A")
  53.             .Names.Add Name:=.Cells(i, "A"), RefersTo:=.Range(R.CurrentRegion.Rows(2), R.CurrentRegion.Rows(R.CurrentRegion.Rows.Count))
  54.             '.Names.Add 新增 套餐的定義名稱
  55.             i = i + 1
  56.             Set R = R.End(xlToRight)
  57.         Loop
  58.     End With
  59. End Sub
複製代碼

TOP

回復 8# Jared
   
第一種寫法目前就是選擇完第一個ComboBox1
第二個選項會先呼應空白
只是想要讓他提前先顯示出來第一項

是這樣嗎?  1#表單的程式碼
  1. 'ListIndex = -1: 所選的不在 List清單內
  2. Dim 價格
  3. Private Sub ComboBox1_Change()
  4.     ComboBox2 = ""
  5.     TextBox1 = ""
  6.     If ComboBox1.ListIndex > -1 Then
  7.         ComboBox2 = ComboBox2.List(ComboBox1.ListIndex)
  8.         TextBox1 = 價格(ComboBox1.ListIndex)
  9.     End If
  10. End Sub
  11. '   Private Sub ComboBox2_Change()
  12. '       TextBox1 = ""
  13. '       If ComboBox2.ListIndex > -1 Then TextBox1 = 價格(ComboBox1.ListIndex)
  14. '   End Sub
  15. '****************************************************
  16. Private Sub CommandButton1_Click()
  17.     If ComboBox1.ListIndex = -1 Then MsgBox "餐點內容 需齊全 !!": Exit Sub
  18.     With Sheet10
  19.         .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 3).Value = Array(ComboBox1, ComboBox2, TextBox1)
  20.     End With
  21. End Sub
  22. Private Sub CommandButton2_Click()
  23.     test4.Hide
  24. End Sub
  25. Private Sub CommandButton3_Click()
  26.     ComboBox1 = ""
  27. End Sub
  28. Private Sub UserForm_Initialize()
  29.     With ComboBox1
  30.         .Font.Size = 12
  31.         .TextAlign = fmTextAlignCenter                  '文字置中
  32.         .List = Array("一號餐", "二號餐", "三號餐")
  33.     End With
  34.     With ComboBox2
  35.         .Font.Size = 12
  36.         .TextAlign = fmTextAlignCenter                  '文字置中
  37.         .List = Array("漢堡、薯條、可樂", "雞塊、薯餅、紅茶", "薯餅、薯條、冰淇淋")
  38.         .Locked = True                                  '禁止編輯
  39.     End With
  40.     With TextBox1
  41.         .Locked = True                                  '禁止編輯
  42.         .Font.Size = 12
  43.         .TextAlign = fmTextAlignCenter                   '文字置中
  44.     End With
  45.     價格 = Array(50, 60, 70)
  46. End Sub
複製代碼

TOP

        靜思自在 : 不要隨心所欲,要隨心教育自己。
返回列表 上一主題