0

我正在尝试将纬度和经度转换为 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
4

1 回答 1

1

循环遍历单元格的首选方法是使用for each c in rngand 在您的示例中,我将执行以下操作:

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
于 2013-04-29T09:41:39.033 回答