I'm getting a compiled error for nested ifs in VBA See the code below:
Function checkPrefix(ByVal term As String) As String
Dim prefixarrU, prefixarrL, compareValue
compareValue = (StrComp(term, UCase(term), vbBinaryCompare))
prefixarrU = Array("U", "M", "L")
prefixarrL = Array("e", "m", "l")
If compareValue = 0 Then
'char is uppercase
For x = LBound(prefixarrU) To UBound(prefixarrU)
If (prefixarrU(x) = term And term = "U") Then
checkPrefix = "upper "
ElseIf (prefixarrU(x) = term And term = "M") Then
checkPrefix = "middle "
ElseIf (prefixarrU(x) = term And term = "L") Then
checkPrefix = "lower "
Else
checkPrefix = ""
End If
ElseIf compareValue <> 0 Then
'char is lowercase
For x = LBound(prefixarrL) To UBound(prefixarrL)
If prefixarrL(x) = term And term = "e" Then
checkPrefix = "early "
ElseIf prefixarrL(x) = term And term = "m" Then
checkPrefix = "medial "
ElseIf prefixarrL(x) = term And term = "L" Then
checkPrefix = "late "
Else
checkPrefix = ""
End If
Else
'Error
End If
End Function
- Note sure why I get the error, I did end all the if statements with end if. Some sort of explanation.
CodePudding user response:
Function checkPrefix(ByVal term As String) As String
Dim d As Object: Set d = CreateObject("Scripting.Dictionary")
d.Add "U", "upper"
d.Add "M", "middle"
d.Add "L", "lower"
d.Add "e", "early"
d.Add "m", "medial"
d.Add "l", "late"
checkPrefix = d(term)
End Function
CodePudding user response:
You could take advantage of the "Select Case" statement
Function CheckPrefix(ByVal term As String) As String
Select Case Left$(termi, 1)
Case "U"
CheckPrefix = "upper "
Case "M"
CheckPrefix = "middle "
Case "L"
CheckPrefix = "lower "
Case "e"
CheckPrefix = "early "
Case "m"
CheckPrefix = "medial "
Case "l"
CheckPrefix = "late "
Case Else
CheckPrefix = vbNullString
End Select
End Function