在VBA中从垂直范围的单元格创建多个从属下拉列表

时间:2016-10-24 12:30:06

标签: vba dropdown

我有一个从一个范围拉出的下拉框

'Type Dropdown box
With Range("B3:D3").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=Sheet2!A2:A32"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

我正在尝试制作多个依赖的下拉框,用我对特定单元格的方式索引单元格:

=INDEX(SOURCE, MATCH(B3,TYPES, 0))

问题是我无法找到引用和索引垂直范围的方法。我的尝试看起来像这样:

Dim TYPES_COLUMN As Range
Dim TYPES As String
Dim SELECTED_TYPE_CELL As Range
Dim SELECTED As String
Dim OPTIONAL_ROW As Range
Dim SUBOPTION As String
 Set TYPES_COLUMN = Worksheets("Sheet2").Range("A2:A32")
  ThisWorkbook.Names.Add Name:="TYPES", RefersTo:=TYPES_COLUMN
 Set SELECTED_TYPE_CELL = Worksheets("Sheet1").Range("B3")
  ThisWorkbook.Names.Add Name:="SELECTED",RefersTo:=SELECTED_TYPE_CELL
 Set OPTIONAL_ROW = Worksheets("Sheet2").Range("R2:W32")
  ThisWorkbook.Names.Add Name:="SUBOPTION", RefersTo:=OPTIONAL_ROW

可选行范围是一个块,但我只是尝试选择R2:W2,例如根据B3中父下拉框中为A2选择的类型列中的计数器部分。

'Options drop down box
With Range("A15").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=INDEX(SUBOPTION,MATCH(SELECTED,TYPES,0))"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

现在显然这不起作用,我是一个新手,但如果有人能解释我做错了什么,也许如何解决它我会很感激。感谢您的时间和任何帮助。

0 个答案:

没有答案