iT邦幫忙

0

VBA

gxx 2021-06-09 20:57:54790 瀏覽

就是我有兩個下拉式選單
分別是”1“,”2”
1選單裡有A,B
2選單裡有C,D,E,F,G,H
順序是要先決定1選單的A B
如果選擇的是A 那麼2選單出現C,D,E
“ B “ F,G,H
以上我有寫出來了!但問題是
假設
我先點了A 所以第二個選單出現C,D,E
下一筆資料我選擇B 第二個選單除了出現F,G,H還保留了上一筆的C,D,E(應該不能保留的)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
0
blanksoul12
iT邦研究生 5 級 ‧ 2021-06-10 09:16:47

下拉式窗口的 ACTION CHANGE 應該要有第一個窗口的 CHANGE 時要 CLEAR 第二個窗口的內容

0
860715
iT邦新手 2 級 ‧ 2021-06-10 10:00:00

這不用寫VBA吧?
https://ithelp.ithome.com.tw/upload/images/20210610/20007393PKgNZ9daFi.png

  1. 先定義三個名稱CAT、A、B (目標框起來、快速鍵alt+ctl+F3)
    https://ithelp.ithome.com.tw/upload/images/20210610/20007393HXwHk8Pvc0.png

2.欄1的資料驗證設定為:
https://ithelp.ithome.com.tw/upload/images/20210610/20007393PVNI5nUnss.png

3.欄2的資料驗證設定為:
https://ithelp.ithome.com.tw/upload/images/20210610/20007393ZK67UdQZ8T.png

0
Neish
iT邦研究生 1 級 ‧ 2021-06-10 10:02:06

試看看這樣吧

其中1選單 我是放在Range("A1")

清單的寫法 我是參考錄製巨集後的結果

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Row = 1 And Target.Column = 2 Then

        If Range("A1") = "A" Then
        
            With Target.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="C,D,E"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .IMEMode = xlIMEModeNoControl
                .ShowInput = True
                .ShowError = True
            End With
            
        ElseIf Range("A1") = "B" Then
            
            With Target.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="F,G,H"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .IMEMode = xlIMEModeNoControl
                .ShowInput = True
                .ShowError = True
            End With
            
        End If
        
    End If

End Sub

我要發表回答

立即登入回答