【发布时间】:2020-04-27 09:56:02
【问题描述】:
我需要什么
两列,每一行都有一个下拉列表,我可以在其中选择代码,另一列每行都有一个下拉列表,将根据该列表选择显示子代码。
我有什么
用 VBA 代码填充所有 J 列的代码:
Sub main()
Dim lCopyLastRow As Long
lCopyLastRow = Workbooks("Reports.xlsm").Worksheets("Data").Cells(Workbooks("Reports.xlsm").Worksheets("Data").Rows.Count, "AT").End(xlUp).Row
'replace "J2" with the cell you want to insert the drop down list
With Range("J2").Validation
.Delete
'replace "=A1:A6" with the range the data is in.
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=Data!$AT$2:$AT$" & lCopyLastRow
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
当我尝试对相关下拉列表执行相同操作时,我收到错误。
Sub main2()
Dim lCopyLastRow As Long
'replace "J2" with the cell you want to insert the drop down list
With Range("K2").Validation
.Delete
'replace "=A1:A6" with the range the data is in.
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=OFFSET(Data!$E$1,MATCH($J2,Data!$C$2:$C$6253,0),0,COUNTIF(Data!$C$2:$C$6253,$J2))"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
我知道问题出在公式上。
【问题讨论】:
-
您是否尝试过手动对一行进行此操作?
-
你得到什么样的错误?哪条线抛出它?
-
另外,很高兴看到使用数据的样本,因为我无法理解这个
=OFFSET(Data!$E$1,MATCH($J2,Data!$C$2:$C$6253,0),0,COUNTIF(Data!$C$2:$C$6253,$J2)) -
@cybernetic.nomad 如果我使用 excel 中的验证按钮在一行中执行此操作,我不会收到错误消息,但如果使用 VBA,我会收到错误消息。
-
@我收到此错误:imgur.com/d9E3mzT,这是出现错误的行:imgur.com/WVAHuZp