使此代码循环直到最后一个非空行

时间:2015-01-22 01:31:25

标签: excel vba excel-vba

我做的所有事情都包含了一个循环破坏了代码 我尝试在迭代行中使用For Loop,但它在userid行上出错。我只需要做20次,共20行。

Sub Testbitly()
    Dim userid As String
    Dim apiKey As String
    Dim shortURL As String
    Dim URLcompleto As String

    userid = Sheets("Plan1").Cells(7, 6)
    apiKey = Sheets("Plan1").Cells(7, 7)
    URLcompleto = Sheets("Plan1").Cells(7, 8)

    shortURL = ShortenURL(userid, apiKey, _
        URLcompleto, bitly)
    Range("I7").Select
    ActiveCell.FormulaR1C1 = shortURL
End Sub

1 个答案:

答案 0 :(得分:1)

Sub Testbitly()

 Dim userid As String
 Dim apiKey As String
 Dim shortURL As String
 Dim URLcompleto As String
 Dim LastRow As Long

 LastRow = Sheets("Plan1").Cells(.Rows.Count, "F").End(xlUp).Row

 For i = 7 To LastRow
      userid = Sheets("Plan1").Cells(i, 6)
      apiKey = Sheets("Plan1").Cells(i, 7)
      URLcompleto = Sheets("Plan1").Cells(i, 8)

      shortURL = ShortenURL(userid, apiKey, _
    URLcompleto, bitly)
      Sheets("Plan1").Cells(i, 9).FormulaR1C1 = shortURL
 Next i

End Sub