简体   繁体   English

Excel VBA-将数据复制到同一工作表时出现问题

[英]Excel VBA - Issue copying data to the same Worksheet

I'm trying to read table Values (imported from a URL in Column A) on Sheet1 & then paste the table values on Sheet2, Each underneath the other in the row directly beneath the previously pasted table value in Column B on Sheet2. 我正在尝试读取Sheet1上的表值(从A列中的URL导入),然后将表值粘贴到Sheet2上,每个值都位于Sheet2的B列中先前粘贴的表值正下方的行中。 These pasted row values will usually change each time (always 10 Columns across & a variable # of Rows). 这些粘贴的行值通常每次都会更改(始终为10列且行数为可变)。

The Code Below will paste each Value 10 Columns over to the right on Sheet2, instead of into Column B (Sheet2), & the correct row. 下面的代码会将每个“值10列”粘贴到Sheet2的右侧,而不是粘贴到B列(Sheet2)和正确的行中。

Any suggestions are much appreciated. 任何建议,不胜感激。

Here are some sample URL's http://hosteddb.fightmetric.com/fighters/details/994 http://hosteddb.fightmetric.com/fighters/details/993 这是一些示例URL的http://hosteddb.fightmetric.com/fighters/details/994 http://hosteddb.fightmetric.com/fighters/details/993

Sub Macro2()

' Macro2 Macro

Dim WSO As Worksheet
Set WSO = ActiveSheet
Dim WS2 As Worksheet: Set WS2 = Worksheets("Sheet2")
NextRow = WS2.Range("A" & Rows.Count).End(xlUp).Row
'ctr = 1

ActiveWorkbook.Worksheets.Add

For Each cell In WSO.Range("A1:A7") 'There are over 2000 values in Column A
ThisURL = "URL;" & cell.Value
Application.CutCopyMode = False
Range("A1").Select
Selection.Copy
Application.CutCopyMode = False
Range("A1").Select
Set WS2 = ActiveSheet
With ActiveSheet.QueryTables.Add(Connection:= _
    ThisURL, Destination:=WS2.Range("A" & NextRow))
    ' Range("A$" & NextRow))    Different variations i've tested
    ' Cells(ctr + 5, 1))        Different variations i've tested
    .Name = "998"
    .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 = "9"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
 'ctr = ctr + 1
 End With

 Next cell
 End Sub

When you insert each new query table from the web, it is inserting columns each time. 当您从网上插入每个新查询表时,它每次都在插入列。 That's why it kept pushing each added table over to the right. 这就是为什么它不断将每个添加的表推到右侧。 You can use 您可以使用

RefreshStyle=xlOverwriteCells

to keep overwriting the table on a dummy sheet (can even be hidden if needed). 以便继续覆盖虚拟工作表上的表格(如果需要,甚至可以将其隐藏)。 In between each overwrite, copy and paste the data from the dummy sheet and put it on your desired output sheet. 在每次覆盖之间,复制并粘贴虚拟表中的数据,然后将其放在所需的输出表中。 I've adapted your code above to do just that. 我已经在上面修改了您的代码来做到这一点。 It uses Sheet3 as the dummy sheet. 它使用Sheet3作为哑表。

Sub copyFightStats()

Dim wsO As Worksheet
Set wsO = ActiveSheet
Dim ws2 As Worksheet: Set ws2 = Worksheets("Sheet2")
Dim ws3 As Worksheet: Set ws3 = Worksheets("Sheet3")
Dim iTableRow As Integer
Dim iTableCol As Integer
Dim iCopyRow As Integer
Dim iURLRow As Integer
Dim sURL As String
Dim xlURLRange As Range
Dim xlCell As Range

iURLRow = wsO.Range("A1").End(xlDown).Row
Set xlURLRange = wsO.Range("A1:A" & iURLRow)

For Each xlCell In xlURLRange 'There are over 2000 values in Column A

    sURL = "URL;" & xlCell.Value
    Application.CutCopyMode = False
    With ws3.QueryTables.Add(Connection:= _
        sURL, Destination:=ws3.Range("A1"))
        .Name = "998"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "9"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
     End With

    'This part grabs the "length" and "width" of the imported data. Tweak this as needed
    iTableRow = ws3.Range("C1").End(xlDown).Row
    iTableCol = ws3.Range("B1").End(xlToRight).Column
    ws3.Range(Cells(1, 2).Address, Cells(iTableRow, iTableCol).Address).Copy

    If ws2.Range("b1").Value = "" Then
       iCopyRow = 1
    Else
       If ws2.Range("b2").Value = "" Then
           iCopyRow = 2
       Else
            iCopyRow = ws2.Range("b1").End(xlDown).Row + 1
       End If
    End If
    ws2.Range("A" & iCopyRow).PasteSpecial xlPasteAll

    'Clear the contents of the previously imported data before importing the next url's data
    ws3.Range(Cells(1, 2).Address, Cells(iTableRow, iTableCol).Address).ClearContents
 Next xlCell
 End Sub

Let me know if that works for you. 让我知道这是否适合您。

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

相关问题 Excel VBA - 将基于两个参数的数据复制到另一个工作表中 - Excel VBA - Copying data based on two parameters into another worksheet VBA Excel 复制 Visio 当前工作表绘图并粘贴到 Excel 工作表中 - VBA Excel copying the Visio current worksheet drawing and paste in Excel worksheet Excel VBA:将工作表复制到现有工作簿(变量) - Excel VBA: Copying Worksheet to Existing Workbook (variable) 使用VBA将小计复制到Excel中的新工作表 - Copying subtotal to a new worksheet in Excel using VBA VBA将Excel工作表复制到另一个工作簿中的新工作表 - VBA copying an Excel worksheet to a new worksheet in another workbook Excel VBA-将特定列从一个工作表复制到另一工作表 - Excel VBA - Copying specific columns from one worksheet to another worksheet 在与源数据相同的工作表中创建数据透视表 (Excel VBA) - Create pivot table in the same worksheet as the source data (Excel VBA) Excel VBA:循环并在一个范围内查找多个值,然后在另一个工作表的分类列表中复制同一范围内的偏移单元格 - Excel VBA: Loop and finding multiple values in a range then copying the offset cells in the same range in a categorized list in another worksheet VBA:将数据复制到检查记录是否存在的工作表 - VBA: Copying data to a worksheet that checks if record exists Excel Vba工作表数据更改 - Excel Vba Worksheet data change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM