1

I continue to get a syntax error with the following code. It doesn't seem to like my formula, even though it works when I just paste it in the custom data validation.

Formula:

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

VBA Code:

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
5
  • 1
    all " inside the string must be doubled. Commented Mar 23, 2018 at 19:12
  • "=IF(B3="""",TRUE,IF(ISERROR(SUMPRODUCT(SEARCH(MID(B3,ROW(INDIRECT(""15:""&LEN(B3))),15),""0123456789""&""abcdefghijklmnopqrstuvwxyz""))),FALSE,TRUE))" Commented Mar 23, 2018 at 19:12
  • Unfortunately when I do that I get a run time error 1004 - Application-defined or object-defined error. I also tried pasting that code in the cell and I get an error as well. Commented Mar 23, 2018 at 19:15
  • @Daniel The formula with double " wouldn't work in a normal cell in excel, this is just how you have to write it when insert a formula via VBA. Commented Mar 23, 2018 at 19:17
  • I inputted it in to the VBA code an got the runtime error Commented Mar 23, 2018 at 19:20

1 Answer 1

1

Double up all quotes within a quoted string. You can also use text(,) as a substitute for any "" zero-length strings so you don't end up with """".

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
Sign up to request clarification or add additional context in comments.

6 Comments

Just copied and pasted that code. Still same runtime error.
@ScottCraner Can you paste the code and show me? I took my original code and pasted that piece and get the error still. Maybe a VBA setting?
add .Delete to delete the validation that exists prior to adding.
@ScottCraner - Good point. There may be a residual data validation from previous attempts.
@Daniel when this works for you make sure you mark it as correct, as it will remove it from the unanswered queue and reward Jeeped. You mark it as correct by clicking the check mark by the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.