vba - Excel VBA - 将正确的参数传递给过程

标签 vba excel vbscript

我从昨天开始就一直在做这个,但似乎找不到我的路。

在下面的过程中,我创建了 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/

相关文章:

vba - SQLBulkCopy.WriteToServer DataTable 将部分数据写入表的方法?

vba - Excel VBA : Copy Sheet from a workbook to another workbook in different location

Excel VBA - 错误解析日期,处理字符串

.Net 替换 VBScript (interop.msscriptcontrol.dll) DLL

visual-studio - 使用 Visual Studio 而不是内置 VBA 编辑器

对另一个 Excel 工作表使用的表进行排序会导致不同的 VLOOKUP 结果

excel - PowerPoint VBA 在运行期间无法识别 Excel 表格

excel - 将数据从一个工作簿复制到另一个工作簿

http - 在 vbscript 中检索多个 cookie

javascript - 切换 .hta 中的表 <td>