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

时间:2015-08-14 07:45:13

标签: excel vba excel-vba vbscript

我从昨天起就一直在这,但似乎找不到我的方式。

在下面的过程中,我创建了obj。 dropdownRange作为参数传递给底部的过程。但是,两个参数在“即时窗口”中输出为空。当我调试它们时。

我没有在线收到错误 - >但是,使用curCell.Validation,我确实在下一行收到以下错误 - > 。添加类型......

错误:1004:应用程序定义或对象定义的错误

Dim targetAddress As String
targetAddress = "Misc!" & targetList.Address
                   Debug.Print targetAddress
With curCell.Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=targetAddress

***** UPDATE:***** 继续@ExcelDeveloper的评论,我正在尝试以下我认为接近但仍无法正常工作(错误:应用程序或对象定义错误) Sub createDropdownList(ByRef curCell As Range,_     ByVal targetList As Range)

    select distinct(p.id), pd.language, p.Company, pd.shortDescription from dbo.Category c
    join dbo.ProductCategory pc on c.id = pc.CategoryId
    join dbo.Product p on pc.id = p.id
    join dbo.ProductDescription pd on p.id = pd.id
where
    c.Company = 'Normstahl' and 
    c.languageid = 'en' and 
    p.Company = 'Normstahl' and 
    pc.Company = 'Normstahl' and
    c.id != 'Deckenlauf' and 
    pd.language = 'en' and
    pd.Company = 'Normstahl'

2 个答案:

答案 0 :(得分:1)

我假设,formula1需要一个引用,这可能会导致范围或逗号分隔列表(根据MSDN)。但它无法像你那样处理直接给定的范围。

give named range to formula1 in validation.add

答案 1 :(得分:1)

Formula1属性应该是您引用的范围的地址,前面带有等号:

 Formula1 := "=" & targetList.Address