Copying Excel data from multiple worksheets into one single sheet

前端 未结 4 1962
囚心锁ツ
囚心锁ツ 2021-01-16 13:26

I have tried searching the internet for various answers to this question but cannot find the right answer. I have an Excel Workbook with worksheets represent each day of the

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-16 13:48

    Sub CombineSheets()
       Dim ws As Worksheet, wsCombine As Worksheet
       Dim rg As Range
       Dim RowCombine As Integer
    
       Set wsCombine = ThisWorkbook.Worksheets.Add(ThisWorkbook.Worksheets(1))
       wsCombine.Name = "Combine"
    
       RowCombine = 1
       For Each ws In ThisWorkbook.Worksheets
          If ws.Index <> 1 Then
             Set rg = ws.Cells(1, 1).CurrentRegion
             rg.Copy wsCombine.Cells(RowCombine, 2)
             wsCombine.Range(Cells(RowCombine, 1), Cells(RowCombine + rg.Rows.Count - 1, 1)) = ws.Name
             RowCombine = RowCombine + rg.Rows.Count
          End If
       Next
       wsCombine.Cells(1, 1).EntireColumn.AutoFit
       Set rg = Nothing
       Set wsCombine = Nothing
    End Sub
    

提交回复
热议问题