Looping through specific sheets in an excel work b

2019-08-18 02:02发布

I'm trying to copy information from several specific sheets in a workbook, without copying information from irrelevant sheets, to a single sheet called Merge. The name of the sheets where i want to copy the information from is: Summary, Summary(1)... Summary(n+1). In addition, i want the copied information to be pasted after the last row with information and without deleting the header line.

The code i'm using is a mix and match from various answers in different Excel-VBA forums so it's not elegant and probably has lots of errors caused by my limited understanding of VBA and coding as a whole.

This is the code i currently have:

Sub Copy_1()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

'Deleting the information from sheet ñéëåí
Sheets("Merge").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents

'loop through all worksheets and copy the data to the DestSh
For Each sh In ThisWorkbook.Worksheets

    'Loop through all worksheets except the Merge worksheet and the
    'Information worksheet, you can add more sheets to the array if you want.
   If IsError(Application.Match(sh.Name, _
                                 Array(DestSheet.Name, "Merge"), 0)) Then
'fill in the Source Sheet and range
'Set SourceRange = Sheets("Summary").Range("A2:L100")
Set SourceRange = sh.Range("A2:N100")
SourceRange.Copy

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("øéëåæ")
Lr = LastRow(DestSheet)

'With the information from the LastRow function we can
'create a destination cell and copy/paste the source range
Set DestRange = DestSheet.Range("A" & Lr + 1)
'Set DestRange = DestSheet.Range("A" & Last + 1)
'End If

'SourceRange.Copy DestRange
SourceRange.Copy
        With DestSheet.Cells(2, Last + 1)
            .PasteSpecial 8    ' Column width
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With
Next

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

End Sub

I would greatly appreciate your help as i've already spent hours going through various answers on similar issues in different forums and trying to solve this on my own.

Thanks a lot!

1条回答
家丑人穷心不美
2楼-- · 2019-08-18 02:55

Check it out,

Sub Button1_Click()
    Dim ws As Worksheet, sh As Worksheet, pRng As Range
    Set ws = Sheets("Merge")

    For Each sh In Sheets
        If sh.Name <> ws.Name Then    'if sheet name does not ="Merge"
            If sh.Name <> "Jimmy Changa" Then    'just an example if you didn't want to include a sheet
                sh.Range("A2:N100").Copy
                Set pRng = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                pRng.PasteSpecial Paste:=xlPasteFormats
                pRng.PasteSpecial Paste:=xlPasteValues
            End If
        End If
        Application.CutCopyMode = 0
    Next sh
    Columns("A:G").EntireColumn.AutoFit
End Sub
查看更多
登录 后发表回答