我正在尝试编写函数,它将根据布尔值为您提供从列表到给定坐标或状态的最近城市。这就是我所拥有的
Function GetCity(x As String, Optional b as Boolean) As String
Dim i As Long, count As Long, row As Long
Dim min As Double
Count = Range("Table_IVO").Rows.Count
min = 9999999
For i = 1 To Count
If dist(x, Range("Table_IVO")(i,1).Value) < min Then
min = dist(x, Range("Table_IVO")(i,1).Value)
row = i
End If
Next i
If b = True Then
GetCity = Range("Table_IVO")(row, 3).Value
Else
GetCity = Range("Table_IVO")(row, 2).Value
End If
End Function
表格设置如此
A B C
Coords City State
它进入循环但从不循环?它甚至在执行if
之前就已经死了答案 0 :(得分:0)
If b = True Then
GetCity = Range("Table_IVO")(row, 3).Value
Else
GetCity = Range("Table_IVO")(row, 2).Value
End
虽然通过查看完整的代码可能会更清晰,但如果这正是您正在执行的代码,那么可能您错过了结束时如果 在代码示例的最后一行,如
If b = True Then
GetCity = Range("Table_IVO")(row, 3).Value
Else
GetCity = Range("Table_IVO")(row, 2).Value
End If
根据您执行代码的环境,它可能不会显示编译错误。