从web导入数据:重复循环

时间:2014-08-19 00:41:15

标签: vba excel-vba excel

我有这个有效的VBA脚本:

Sub Basic_Web_Query()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").Value, Destination:=Range("E2"))
.Name = "q?s=goog_2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

End Sub

我希望宏在循环中执行此操作,即转到目的地为E3的A3,目的地为A4的A4 ......等等。

任何帮助将不胜感激

thansk!

2 个答案:

答案 0 :(得分:1)

我喜欢将'offset'用于这些循环:OFFSET(rows down, columns right)。 负值向上和向左移动。

例如:

for p= 1 to 5

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").offset(p-1,0).Value, Destination:=Range("E2").offset(p-1,0))
'other code
End With

next p

答案 1 :(得分:0)

谷歌'循环vba',你会看到很多例子。这是一个:

Dim rw As Long

For rw = 1 To 5
    Range("E" & rw).Value = Range("A" & rw).Value
    'other stuff
Next rw