将行从几张纸复制到一张

时间:2019-01-18 09:37:39

标签: excel vba merge

我正在尝试从表1、2、3等中查找带有“ 2019”的行并将其复制到主表。 在这种情况下,如何在纸张之间切换?我可以使用.For吗?

Sub Copy_To_Another_Sheet_1()
With Worksheets(1).Cells
  Set c = .Find("2019", LookIn:=xlValues, LookAt:=xlPart)
  If Not c Is Nothing Then
    firstResult = c.Address
    Do
      c.Select
      ActiveCell.EntireRow.Copy Destination:=Sheets(2).Range("A" & Rows.count).End(xlUp).Offset(1)
Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstResult
  End If
End With
End Sub

3 个答案:

答案 0 :(得分:0)

 Dim ws as worksheet
 For each ws in worksheets
   if ws.index <> 2 then 'ship second sheet because I think that's your master?
       set c = ws.find .... etc 

       if not c is nothing then
             firstResult = c.Address
            Do
                 c.entire.row.copy ....
                 set c = ws.findnext
            loop until...
        end if
      end if
      next ws

答案 1 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim ws As Worksheet
    Dim strSearch As String
    Dim rngSearch As Range
    Dim LastrowMaster As Long

    'Set what you are looking for
    strSearch = "2019"

    'Loop all sheets
    For Each ws In ThisWorkbook.Worksheets
        'Loop sheets except the master sheet
        If ws.Name <> "Master" Then

            'Set ws as search range & get the row
            Set rngSearch = ws.UsedRange.Find(strSearch)

            'Find master sheet lastrow
            LastrowMaster = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row

            'Paste the copied line in master sheet
            ws.Rows((rngSearch.Row)).EntireRow.Copy wsMaster.Range("A" & LastrowMaster + 1)

        End If

    Next

End Sub

答案 2 :(得分:0)

从所有工作表到主工作表

阵列版本

Sub AllSheetsToMaster()

  Const cMaster As Variant = "Master"   ' Master Worksheet Name/Index
  Const cSearch As String = "2019"      ' Search String
  Const cFirstR As Long = 1             ' Source First Row Number
  Const cFirstC As Variant = 1          ' Source First Column Letter/Number

  Dim vntS As Variant   ' Source Array
  Dim vntC As Variant   ' Count Array
  Dim vntT As Variant   ' Target Array
  Dim LastUR As Long    ' Source Last Used Row Number
  Dim LastUC As Long    ' Source Last Used Column Number
  Dim LastR As Long     ' Target Last Used Row Number
  Dim x As Long         ' Worksheet Counter
  Dim i As Long         ' Source Array Row Counter
  Dim j As Long         ' Source/Target Array Column Counter
  Dim k As Long         ' Target Array Row Counter

  For x = 1 To ThisWorkbook.Worksheets.Count ' Worksheets

    ' From Source Worksheet to Source Array.

    With ThisWorkbook.Worksheets(x)
      ' Check if current worksheet name is th same as Master Worksheet Name.
      If .Name = cMaster Then GoTo NextWorksheet
      ' Check if current worksheet is empty.
      If .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
          Is Nothing Then GoTo NextWorksheet
      ' Calculate Last Used Row and Column in current Source Worksheet.
      LastUR = .Cells.Find("*", , , , , 2).Row
      LastUC = .Cells.Find("*", , , , 2, 2).Column
      ' Copy current used range to Source Array.
      vntS = .Range(.Cells(cFirstR, cFirstC), .Cells(LastUR, LastUC))
    End With

    ' From Source Array to Count Array.

    ' Count Array cannot be bigger than Source Array.
    ReDim vntC(1 To UBound(vntS))
    k = 0 ' Reset Target Array Row Counter!!!
    For i = 1 To UBound(vntS) ' Rows
        For j = 1 To UBound(vntS, 2) ' Columns
            If vntS(i, j) = cSearch Then
                k = k + 1 ' Increase Target Array Row Counter.
                vntC(k) = i ' Write Source Array Row number to Count Array.
                Exit For ' Stop checking as soon as found.
            End If
        Next
    Next
    ReDim Preserve vntC(1 To k) ' Resize Count Array (make it smaller).

    ' From Source Array to Target Array.

    ' Resize Target Array to the Count Array rows and Source Array columns.
    ReDim vntT(1 To k, 1 To j)
    For i = 1 To k ' Rows
        For j = 1 To UBound(vntS, 2) ' Columns
            vntT(i, j) = vntS(vntC(i), j) ' Write to Target Array.
        Next
    Next

    ' From Target Array to Target Range.

    With ThisWorkbook.Worksheets(cMaster)
      ' Calculate Target Last Used Row Number.
      If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
              Is Nothing Then LastR = .Cells.Find("*", , , , , 2).Row
      ' Copy Target Array to Target Range.
      .Cells(LastR + 1, cFirstC).Resize(UBound(vntT), UBound(vntT, 2)) = vntT
    End With

NextWorksheet:
  Next

End Sub