Excel VBA Formula1编译错误

时间:2018-03-23 19:10:49

标签: excel vba excel-vba

我继续使用以下代码获得语法错误。它似乎不喜欢我的公式,即使它只是在我将其粘贴到自定义数据验证中时也能正常工作。

公式:

=IF(B3="",TRUE,IF(ISERROR(SUMPRODUCT(SEARCH(MID(B3,ROW(INDIRECT("15:"&LEN(B3))),15),"0123456789"&"abcdefghijklmnopqrstuvwxyz"))),FALSE,TRUE))

VBA代码:

Public Sub Class_Initialize()
'Dim Range As String

With Range("e5").Validation
 .Add Type:=xlValidateCustom, _
 AlertStyle:=xlValidAlertStop, _
 Operator:=xlBetween, Formula1:="=IF(B3="",TRUE,IF(ISERROR(SUMPRODUCT(SEARCH(MID(B3,ROW(INDIRECT("15:"&LEN(B3))),15),"0123456789"&"abcdefghijklmnopqrstuvwxyz"))),FALSE,TRUE))"
 .InputTitle = "Integers"
 .ErrorTitle = "Integers"
 .InputMessage = "Enter an integer from five to ten"
 .ErrorMessage = "You must enter a number from five to ten"
End With

End Sub

1 个答案:

答案 0 :(得分:1)

将引用字符串中的所有引号加倍。您还可以使用text(,)代替任何""零长度字符串,这样您就不会以""""结束。

With Range("e5").Validation
     .delete
     .Add Type:=xlValidateCustom, _
     AlertStyle:=xlValidAlertStop, _
     Operator:=xlBetween, Formula1:="=IF(B3=text(,), TRUE,IF(ISERROR(SUMPRODUCT(SEARCH(MID(B3,ROW(INDIRECT(""15:""&LEN(B3))),15),""0123456789abcdefghijklmnopqrstuvwxyz""))),FALSE,TRUE))"
     .InputTitle = "Integers"
     .ErrorTitle = "Integers"
     .InputMessage = "Enter an integer from five to ten"
     .ErrorMessage = "You must enter a number from five to ten"
End With