应用程序对象在"添加类型"中定义的错误在哪里?线?

时间:2017-08-25 17:30:48

标签: excel-vba validation vba excel

Sub Task()
Dim wb, wb1 As Workbook, copyrange, copyrange1, EWSD, Mrg1, Mrg2, Mrg3, Mrg4, SERange, SERange1 As Range, list, str, str1, str2, str3, str4 As String, Rowt As Long
Application.enableEvents = False

    Set wb = ActiveWorkbook
        list = Range("$A$15:$A$18")
        With wb.Sheets("Client_Eligibility Info").Range("C16").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=list
        End With

1 个答案:

答案 0 :(得分:1)

  

Dim wb, wb1 As Workbook, copyrange, copyrange1, EWSD, Mrg1, Mrg2, Mrg3, Mrg4, SERange, SERange1 As Range, list, str, str1, str2, str3, str4 As String, Rowt As Long

将声明所有没有As的变量作为变体

将all更改为显式声明:

Dim wb as workbook,wb1 as workbook,...

确保将list声明为字符串

Dim list as String

然后将地址分配给变量:

'Make sure to assign the correct worksheet to this range
list = wb.WorkSheets("Sheet1").Range("$A$15:$A$18").Address

然后改变

  

Formula1:=list

一个公式。

Formula1:="=" & list

这样:

Sub Task()
Dim wb As Workbook, wb1 As Workbook
Dim copyrange As Range, copyrange1 As Range, EWSD As Range
Dim Mrg1 As Range, Mrg2 As Range, Mrg3 As Range, Mrg4 As Range
Dim SERange As Range, SERange1 As Range
Dim list As String, str As String, str1 As String, str2 As String
Dim str3 As String, str4 As String
Dim Rowt As Long

Application.EnableEvents = False

    Set wb = ActiveWorkbook
        list = wb.Worksheets("Sheet1").Range("$A$15:$A$18").Address
        With wb.Worksheets("Client_Eligibility Info").Range("C16").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & list
        End With