Merging excel spreadsheets into one spreadsheet

前端 未结 2 514
盖世英雄少女心
盖世英雄少女心 2020-12-21 01:19

Okay, I tried to look for similar questions but I didn\'t understand much of what was being discussed since it\'s the first time I\'m looking at Excel\'s VBA editor.

相关标签:
2条回答
  • 2020-12-21 01:58

    Jerry, try this code. I cleaned up your code a bit and made it more efficient to be able to do what you wish. I've made some assumptions based on what your code said which I think are right. If not, comment on this answer and I will tweak if needed.

    Option Explicit
    
    Sub Collate_Sheets()
    
    
       Sheets.Add After:=Sheets(Sheets.Count)
       Dim wks As Worksheet
       Set wks = Sheets(Sheets.Count)
    
       wks.Name = "Sheet3"
    
       With Sheets("Sheet1")
    
        Dim lastrow As Long
        lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
    
        .Range("A1:B" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)
    
       End With
    
       With Sheets("Sheet2")
    
        lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
    
        .Range("A2:B" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1)
    
       End With
    
    
    End Sub
    
    0 讨论(0)
  • 2020-12-21 02:09

    In case anybody wants to delete Shee3 before created it to avoid the Error

       'Delete Sheet 3
       Application.DisplayAlerts = False
       Sheets("Sheet3").Delete
    

    Thanks Scott Holtzman!!

    0 讨论(0)
提交回复
热议问题