我正在尝试将纬度和经度转换为VBA中的度数。我想编写子程序来将整个列重写为度。我希望它遍历整个列,但它只适用于当前单元格。我用偏移来移动细胞。但是没有帮助
Sub autoConvertToDegree()
Dim sign As Integer
Dim position As Integer
Dim temp As String
Dim tok As Variant
Do
sign = 0
position = 0
tok = Null
temp = ActiveCell.Value
sign = IIf(Left(temp, 1) = "-", -1, 1)
position = InStr(temp, "+")
If (position > 0) Then Mid(temp, position) = " "
position = InStr(temp, "-")
If (position > 0) Then Mid(temp, position) = " "
temp = Replace(temp, "'", " ")
temp = Replace(temp, """", " ")
temp = LTrim(temp)
tok = Split(temp, " ")
ActiveCell.Value = sign * (tok(0) + tok(1) / 60# + tok(2) / 3600#)
ActiveCell.Offset(1, 0).Select
Loop While Application.IsText(ActiveCell.Value)
End Sub
答案 0 :(得分:1)
循环遍历单元格的首选方法是使用for each c in rng
,在您的示例中,我将执行以下操作:
Sub autoConvertToDegree()
Dim sign As Integer
Dim position As Integer
Dim temp As String
Dim tok As Variant
For Each c In ActiveSheet.Range("A1:A100")
If Application.IsText(c) Then
sign = 0
position = 0
set tok = Nothing '<~~ set variant to nothing
temp = c
sign = IIf(Left(temp, 1) = "-", -1, 1)
position = InStr(temp, "+")
If (position > 0) Then Mid(temp, position) = " "
position = InStr(temp, "-")
If (position > 0) Then Mid(temp, position) = " "
temp = Replace(temp, "'", " ")
temp = Replace(temp, """", " ")
temp = LTrim(temp)
tok = Split(temp, " ")
c = sign * (tok(0) + tok(1) / 60# + tok(2) / 3600#)
End If
Next
End Sub