Kent Maquirang
Kent Maquirang

Reputation: 5

Checkbox array with string

I need help on how to use checkbox array , if checkbox is checked the value will be:

check1(0) = OK ,check1(1) = X,check1(2) = [X],check1(3) = NP
a=iif(check1(0).Value = vbchecked,"OK")

Getting error:

"Error sub or function not defined"

Upvotes: 0

Views: 155

Answers (1)

Brian M Stafford
Brian M Stafford

Reputation: 8868

I'm not sure how the line of code you presented would generate the error sub or function not defined. Rather, you should have received the errorArgument not optional. The syntax for this line should have been like this:

a = IIf(Check1(0).Value = vbChecked, "OK", "")

If I understand your requirements, you COULD use IIf like this:

a = IIf(Check1(0).Value = vbChecked, "OK", IIf(Check1(1).Value = vbChecked, "X", IIf(Check1(2).Value = vbChecked, "[X]", IIf(Check1(3).Value = vbChecked, "NP", ""))))

However, I would not recommend this approach. Instead, I would structure the code like this:

If Check1(0).Value = vbChecked Then
   a = "OK"
ElseIf Check1(1).Value = vbChecked Then
   a = "X"
ElseIf Check1(2).Value = vbChecked Then
   a = "[X]"
ElseIf Check1(3).Value = vbChecked Then
   a = "NP"
Else
   a = ""
End If

Yes, there is a little more code but the code is easier to read and easier to maintain.

Upvotes: 1

Related Questions