从第一个空行开始,在VBA中的For..Next Loop中声明variablescounter

下面的代码是一个简单的公式中继器。 我有一个工作正常,但我想离开selectactivate并开始声明variables,使代码更容易阅读。

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

我真正需要帮助的是理解For..Next循环。 对于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 ie 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 

PS不用担心公式itsel,它自己的工作。 是的,我可以把它拖下来,但我正在努力练习。 获取定义rowcount ATM的types不匹配。

希望你正在寻找这个

 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