我从昨天开始就一直在做这个,但似乎找不到我的路。
在下面的过程中,我创建了 obj. dropdownRange 作为参数传递给底部的过程。但是,当我 debug.print 时,这两个参数在“立即窗口”中输出为空。
我没有收到错误 -> 使用 curCell.Validation,但是,我在下一行收到以下错误 -> .Add Type...
错误:1004:应用程序定义或对象定义的错误
Dim mainTab As Object 'Tab 'main'
Dim tranTypeSize As Integer 'Length of entries for transaction type
Dim dropdownRange As Range 'Dynamic range for dropdown
Set mainTab = Sheets("Main")
tranTypeSize = Sheets("Misc").Cells(Rows.Count, 1).End(xlUp).Row
Set dropdownRange = Sheets("Misc").Range("A1:A" & tranTypeSize) ' <- problem
Call createDropdownList(mainTab.Range("G8"), dropdownRange) '<- arguments passed are wrong obj?
----------------------------------------------------------------
Sub createDropdownList(ByRef curCell As Range, _
ByVal targetList As Range)
With curCell.Validation ' <- I think error, though it passes this line
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=targetList ' <- error
*****更新:***** 除了@ExcelDeveloper的评论之外,我正在尝试以下我认为接近但仍然不起作用的操作(错误:应用程序或对象定义的错误) Sub createDropdownList(ByRef curCell As Range, _ ByVal targetList 作为范围)
Dim targetAddress As String
targetAddress = "Misc!" & targetList.Address
Debug.Print targetAddress
With curCell.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=targetAddress
最佳答案
Formula1 属性应该是您所引用的范围的地址,前面带有等号:
Formula1 := "=" & targetList.Address
关于vba - Excel VBA - 将正确的参数传递给过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004844/