将数据从多个工作表复制到Excel VBA中的主工作表中

时间:2018-07-02 12:22:38

标签: excel vba excel-vba

我需要将同一工作簿中sheet2的单元格(A14)之后的所有单元格内容添加到sheet13,直到sheet13(数据范围是未知的,取决于外部来源),并且每张工作表的名称应出现在摘要表作为标题。

任何帮助将不胜感激。

Sub Copy_Sheets_To_Master()
Application.ScreenUpdating = False
Dim i As Long
Dim ans As String
Dim Lastrow As Long
Dim Lastrowa As Long
Dim Lastrowd As Long

    For i = 2 To Sheets.Count

        ans = Sheets(i).Name
        Lastrowa = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
        Sheets(i).Range("A14" & Lastrowa).Copy Sheets("Master").Range("A" & Lastrow)
        Lastrowd = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
        Sheets("Master").Range("D" & Lastrow & ":D" & Lastrowd).Value = ans
        Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1

    Next
Application.ScreenUpdating = True
End Sub

欢呼

1 个答案:

答案 0 :(得分:0)

尝试如下所示:

Sub Copy_Sheets_To_Master()

Dim wb As Workbook
Dim ws As Worksheet
Dim i, LastRowa, LastRowd As Long
Dim WSname As String

Set wb = ActiveWorkbook

For Each ws In wb.Sheets

If ws.Name <> "Master" Then

WSname = ws.Name

LastRowa = ws.Cells(Rows.Count, "A").End(xlUp).Row
LastRowd = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row

ws.Range("A14:A" & LastRowa).Copy
Sheets("Master").Range("A" & LastRowd + 2).PasteSpecial Paste:=xlPasteValues
Sheets("Master").Range("A" & LastRowd + 1).Value = WSname

End If

Next ws

End Sub
相关问题