1

我录制了一个宏并对其进行了编辑。它从网页中抓取特定数据(在链接上找到)并将它们显示在单独的页面上。

我想使用数据(VLOOKUP),但数据位于不同的页面上,因此很难获得准确的公式。

每周我都会更改代码的第二行

For x = 1 To 20

For x = 21 to ....

例如,因为每周都会出现新的链接/数据。

如何找到最后一行以在其下方添加下一批数据?

Sub Update()
    For x = 1 To 20
        Worksheets("Links").Select
        Worksheets("Links").Activate
        mystr = Cells(x, 8)
        mystr2 = Cells(x, 15)
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = x
        With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$K$1"))
            .Name = "report2_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlAllTables
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    
        Range("A1").Select
        With ActiveSheet.QueryTables.Add(Connection:=mystr2, Destination:=Range("$A$1"))
            .Name = "report6_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlAllTables
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
        End With
    Next x
End Sub
4

1 回答 1

0

代替

for x = 1 to 20

for x = Range("A" & Rows.Count).End(xlUp).Row to Range("A" & Rows.Count).End(xlUp).Row-20 step -1

这将找到 A 列中使用的最后一个单元格并从中减去 20 行并迭代反向

于 2013-09-03T11:20:15.860 回答