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