从第一个空白行开始,并为for中的计数器声明变量.VBA中的下一个循环



下面的代码是一个简单的公式中继器。我有一个工作得很好,但我想放弃selectactivate,开始声明变量,这样代码更容易阅读。

我基本上想按下快捷键,让代码从F列的第一个空白行开始(从F4开始)。然后,我希望代码使用以下格式的值输入公式:formula(ValueTwoColumnsLeft,ValueOneColumnLeft)。

我真正需要帮助的是理解。。下一个循环。对于For StartCell = 1 To 2500部分,我不明白i应该是什么,也不明白我应该如何定义它。我知道它不能是一个范围,那么它就没有意义了。

Sub FormulaRepeater()
' Keyboard Shortcut: Ctrl+q
Dim sht As Worksheet
Dim Address As Range
Dim rowcount As Long
Dim Latitude As String
Dim Longitude As String
Dim result As String
Dim StartCell As Range
Set sht = Worksheets("S2")
Set Address = sht.Range("F4").End(xlDown)
rowcount = sht.Cells(Rows.Count, Address) 'start from first blank row in F
Set StartCell = sht.Cells(rowcount + 1, Address)
Latitude = Cells.Offset(0, -2).Value ' get latitude 2 cells to the left
Longitude = Cells.Offset(0, -1).Value 'get longitude 1 cell to the left
'Would using offset here be the same as select i.e. sets Application.ScreenUpdating=True
For rowcount = StartCell To 10 'from the selected cell to 2500, _
which is the daily limit for the Google API
        result = GEOAddress(Latitude, Longitude)

    Next
End Sub

第页。S不用担心这个公式本身,它自己起作用。是的,我可以把它拖下来,但我正在努力练习。在定义行计数ATM时获取类型不匹配。

希望您正在寻找此

Sub FormulaRepeater()
    Dim sht As Worksheet
    Dim rowcount As Long
    Dim Latitude As String
    Dim Longitude As String
    Dim result As String
    Dim StartCell As Long
    Set sht = Worksheets("S2")
    StartCell = sht.Range("F4").End(xlDown).Row
    rowcount = sht.Range("F" & Rows.Count).End(xlUp).Row 'start from first blank row in F
    For i = StartCell To rowcount
        Latitude = sht.Range("F" & i).Offset(0, -2).Value ' get latitude 2 cells to the left
        Longitude = sht.Range("F" & i).Offset(0, -1).Value 'get longitude 1 cell to the left
        result = GEOAddress(Latitude, Longitude)
    Next
End Sub

最新更新