【问题标题】:Copy paste using loop from multiple ranges to single row into another WB使用循环从多个范围复制粘贴到单行到另一个 WB
【发布时间】:2020-11-18 07:24:18
【问题描述】:

我正在尝试将数据从多个源文件复制到目标文件中。 所以一个文件夹包含了我收到的所有源文件。

我现在必须将收到的文件中的数据整理到一个工作簿中。

Source file Destination file/Collation file

我正在尝试从文件夹中的每个源文件整理到目标文件中获得一些帮助。

子 Transfer_data() 将 wb 变暗为字符串 暗淡我只要 暗淡 j 只要 暗淡无光

Application.ScreenUpdating = False

 i = 0
 j = 0

wb = Dir(ThisWorkbook.Path & "\*")
Do Until wb = ""

If wb <> ThisWorkbook.Name Then

Workbooks.Open ThisWorkbook.Path & "\" & wb

With Workbooks(wb).Sheets("D. P & c data")
   

For i = 21 To 26
For j = 3 To 60 Step 10

.Range(Cells(i, 3), Cells(i, 12)).Copy ThisWorkbook.Sheets("P and c data").Cells(Rows.Count, j).End(xlUp).Offset(1)


Next j

Next i


End With

Application.CutCopyMode = False
Workbooks(wb).Close True

End If
wb = Dir
Loop

Application.ScreenUpdating = True
MsgBox " Copy Complete"

结束子

【问题讨论】:

  • 你能edit你对你正在尝试的代码的问题吗?
  • 欢迎来到 SO。因此,您尝试将数据从C21:L23 逐行复制到C3:C32C55 适合在哪里,或者您的意思是将数据复制到 C26:C55?请澄清一下。
  • 感谢 BigBen 和 VBasic2008。我已经添加了我的代码。
  • @VBasic2008 - 抱歉,c55 可能令人困惑,但我正在尝试将粘贴数据从 C21:L21 复制到范围 C3:L3 上的另一个 WB,下一行 C22:L22 需要继续将在目标 WB 上更新,范围从 M3:V3 等继续。
  • 如果您愿意返回代码。没有它,用户将无法欣赏您为此付出了多少努力。

标签: excel vba


【解决方案1】:

我不确定您的代码在循环之前和之后发生了什么。我认为下面的循环是你正在寻找的。将行放在列之外更容易。


For i = 21 To 26
    For j = 3 To 13
        Dim lr As Long
            lr = ThisWorkbook.Sheets("P and c data").Range("C" & Rows.Count).End(xlUp).Row + 1
                Cells(i, j).Copy
                Sheets("P and c data").Cells(lr, 3).PasteSpecial

    Next j
Next i

【讨论】:

  • 谢谢@roses56。它不起作用,因为它们是两个不同的文件,并且工作表名称也不同。我想我可能弄错了代码。
【解决方案2】:

按行复制范围到单行

Option Explicit

' Copies values from a specified range (srcAddr)
' in a specified worksheet (srcID) in all workbooks ("*.xls*") in the folder
' of ThisWorkbook (ThisWorkbook excluded), to a specified worksheet (tgtID)
' in ThisWorkbook. The values of the range are copied into a single row
' starting from a specified column (tgtCol), each row of the range next
' to the previous.
Sub transferData()
    
    Const srcID As Variant = "D. P & c data" ' Name or Index e.g. "Sheet1" or 1
    Const srcAddr As String = "C21:L26"
    Const tgtID As Variant = "P and c data"  ' Name or Index e.g. "Sheet1" or 1
    Const tgtCol As Variant = 3              ' Number or String e.g. 1 or "A"
    Const Pattern As String = "*.xls*"
    
    Dim wbPath As String: wbPath = ThisWorkbook.Path & Application.PathSeparator
    Dim tgt As Worksheet: Set tgt = ThisWorkbook.Worksheets(tgtID)
    
    Application.ScreenUpdating = False
    
    Dim wb As Workbook, src As Worksheet, tgtCell As Range ' Objects
    Dim Source As Variant, Target As Variant               ' Arrays
    Dim i As Long, j As Long, l As Long, Count As Long     ' Counters (Longs)
    
    Dim wbname As String: wbname = Dir(wbPath & Pattern)
    Do Until wbname = ""
        If wbname <> ThisWorkbook.Name Then
            GoSub readSource
            GoSub writeSource
            GoSub writeTarget
        End If
WorksheetNotFound:
        wbname = Dir
    Loop
    
    Application.ScreenUpdating = True
    
    MsgBox "Copied data from " & Count & " workbook(s) containing " _
           & "a worksheet ID-ed with '" & srcID & "'.", _
           vbInformation, "Data Transfer"
  
Exit Sub

readSource:
    ' Write values from Source Range to Source Array.
    On Error Resume Next
    Set src = Workbooks.Open(wbPath & wbname).Worksheets(srcID)
    If Err.Number <> 0 Then GoTo closeSourceError
    On Error GoTo 0
    Source = src.Range(srcAddr).Value
    ' Uncomment the following line to write the names of the worksheets
    ' and the workbooks (that were read from) to the Immediate window (CTRL+G).
    Debug.Print src.Name, src.Parent.Name
    src.Parent.Close False ' Just reading, no need to save.
    Return
    
writeSource:
    ' Write values from Source Array to Target Array.
    ReDim Target(1 To 1, 1 To UBound(Source) * UBound(Source, 2))
    l = 0
    For i = 1 To UBound(Source)
        For j = 1 To UBound(Source, 2)
            l = l + 1
            Target(1, l) = Source(i, j)
        Next j
    Next i
    Return

writeTarget:
    ' Write values from Target Array to Target Range.
    Set tgtCell = tgt.Cells(tgt.Rows.Count, tgtCol).End(xlUp).Offset(1)
    tgtCell.Resize(, UBound(Target, 2)).Value = Target
    Count = Count + 1
    Return

closeSourceError:
    src.Parent.Close False ' Just reading, no need to save.
    On Error GoTo 0
    GoTo WorksheetNotFound
  
End Sub

【讨论】:

  • 感谢@VBasic2008。我已经在问题中添加了我的代码。我已经尝试过您的代码,但似乎没有复制所有数据。每个源文件都需要进入目标文件中的单独行,但它似乎不像那样工作。你能帮忙吗?
  • 我附上了使用您提供的代码的结果截图,但似乎无法理解如何修复它。源文件中的行应作为块粘贴到目标文件的连续列中。此外,每个文件都需要在目标文件的新行中出现。你能帮忙吗?
  • 我对写源代码做了一些修改,现在可以完美运行了。谢谢。
  • @Debbie A:赞美女王。我很高兴回答了你的问题,也很高兴你找到了我不可思议的mistake in the loops。最好的学习方法是自己解决问题。干得好。
  • 谢谢!我现在对代码有不同的问题。当新文件上的第一个值为空时,数据有空白而不是值或零,则新文件中的下一组数据与最后一组数据显示在同一行中。那么有没有办法替换来自源的数据中的空白值,以便在目标文件中有空白的地方有零?请帮忙!
猜你喜欢
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多