简体   繁体   English

循环宏以创建一个新工作表,对其进行重命名,从网络中添加数据,然后循环返回直到完成

[英]Looped macro to create a new sheet, rename it, add data from the web, then loop back through until complete

Trying to create a macro to take a spreadsheet with two columns (one with names, the other with a URL to a sheet I need data from), create a new sheet for each row, rename that sheet according to the name in column A, then create a web query in the new sheet according to the URL in column B. 尝试创建一个宏以使用包含两列的电子表格(一个带有名称,另一个带有指向我需要数据的工作表的URL),为每行创建一个新工作表,并根据A列中的名称对该工作表进行重命名,然后根据B列中的URL在新工作表中创建一个Web查询。

Here's the macro I tried compiling, but it's not working. 这是我尝试编译的宏,但无法正常工作。

Sub CreateSheetsFromAList()

    Dim wb As Workbook
    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim MyCell As Range, MyRange As Range, URLCell As Range

    Set wb = ThisWorkbook
    Set src = wb.Sheets("AllPlayers")
    Set MyRange = Sheets("AllPlayers").Range("A1")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))
    Set URLCell = Sheets("AllPlayers").Range("B1")
    Set URLCell = Range(URLCell, URLCell.End(xlDown))

    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        With ActiveSheet.QueryTables.Add(Connection:=URLCell, Destination:=Range("$A$2"))
        .Name = "2015"
        .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 = """pgl_basic"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    Next MyCell
End Sub

Made some syntax tweaks that should help as well as error handling if the sheet name already exists. 进行了一些语法调整,这些调整应该会有所帮助,并且在工作表名称已经存在的情况下进行错误处理。 Didn't make any changes to the QueryTable.Add method though. 虽然没有对QueryTable.Add方法进行任何更改。

Sub CreateSheetsFromAList()

    Dim wb As Workbook
    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim ActSht As Worksheet
    Dim MyCell As Range, MyRange As Range, URLValue As Variant

    Set wb = ThisWorkbook
    Set src = wb.Sheets("AllPlayers")
    Set MyRange = src.Range("A1:A" & src.Range("A" & src.Rows.Count).End(xlUp).Row)

    For Each MyCell In MyRange
        On Error Resume Next
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        If Err.Number > 0 Then
            Err.Clear
            MsgBox MyCell.Value & " sheet name already exists"
            Exit Sub
        End If
        On Error Goto 0
        URLValue = MyCell.Offset(0, 1).Value
        Set ActSht = Sheets(Chr(34) & MyCell.Value & Chr(34))
        With ActSht.QueryTables.Add(Connection:= "URL;" & URLValue, Destination:=ActSht.Range("A2"))
          .Name = "2015"
          .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 = """pgl_basic"""
          .WebPreFormattedTextToColumns = True
          .WebConsecutiveDelimitersAsOne = True
          .WebSingleBlockTextImport = False
          .WebDisableDateRecognition = False
          .WebDisableRedirections = False
          .Refresh BackgroundQuery:=False
        End With
    Next MyCell
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM