Don
Don

Reputation: 1

VBA/Excel error - Error in value when using StartsWith

The function below prints value South when I call =LRegionName(cell) from excel when the value of the cell is S. But when the value of the cell is N it gives Error in Value in excel. What am I missing?

Function LRegionName(LRegion)
If LRegion = "S" Then
   LRegionName = "South"
ElseIf LRegion.startsWith("N") Then
    LRegionName = "North"
ElseIf LRegion = "E" Then
   LRegionName = "East"
ElseIf LRegion = "W" Then
   LRegionName = "West"
Else
   LRegionName = "Dont know"
End If

End Function

Upvotes: 0

Views: 112

Answers (2)

Gangula
Gangula

Reputation: 7334

You can use Gary's Answer if you're matching just one letter.

If you want to match multiple letters at the beginning, you can use

ElseIf LRegion Like "N*" Then

Here, the "*" means that it can be anything after N.
Similarly if you type "No*", it means it can be anything after "Nor" or "Norman" or "North"

Upvotes: 0

Gary's Student
Gary's Student

Reputation: 96773

Replace:

ElseIf LRegion.startsWith("N") Then

with:

ElseIf Left(LRegion,1) = "N"  Then

Upvotes: 1

Related Questions