简体   繁体   English

VBA/Excel 错误 - 使用 StartsWith 时的值错误

[英]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.当我从 excel 调用 =LRegionName(cell) 时,当单元格的值为 S 时,下面的 function 打印值 South 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

Replace:代替:

ElseIf LRegion.startsWith("N") Then

with:和:

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

You can use Gary's Answer if you're matching just one letter.如果您只匹配一个字母,则可以使用 Gary's Answer。

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.这里,“*”表示它可以是 N 之后的任何内容。
Similarly if you type "No*", it means it can be anything after "Nor" or "Norman" or "North"同样,如果您键入“No*”,则表示它可以是“Nor”或“Norman”或“North”之后的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM