写一段Vba代码,运行后可根据sheet1中a列的值自动在c列形成下拉多选项,比如a列1-3行内容依次为水果、蔬菜,运行代码后水果这行对应的c列出现苹果、香蕉、梨、西瓜的下拉选框,可以选择一样或者多样,蔬菜对应的c列出现白菜、胡萝卜、西红柿、西兰花的下拉选框,可以选择一样或者多样
时间: 2023-05-14 08:05:56 浏览: 156
Sub CreateDropDown()
Dim lastRow As Long
Dim fruitList As Variant
Dim veggieList As Variant
Dim i As Long
lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
fruitList = Array("苹果", "香蕉", "梨", "西瓜")
veggieList = Array("白菜", "胡萝卜", "西红柿", "西兰花")
For i = 1 To lastRow
If Sheets("Sheet1").Cells(i, "A").Value = "水果" Then
With Sheets("Sheet1").Cells(i, "C").Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(fruitList, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
ElseIf Sheets("Sheet1").Cells(i, "A").Value = "蔬菜" Then
With Sheets("Sheet1").Cells(i, "C").Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(veggieList, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
Next i
End Sub
阅读全文