簡體   English   中英

合並確定工作表和工作簿源VBA的Excel工作表和工作簿

[英]Merge excel sheets and workbooks identifying the sheet and workbook source VBA

我有多個具有相同信息的工作簿和工作表,我一直試圖合並所有標識信息源的文件(工作表-工作簿)。

我已經使用了這段代碼,但是它只是合並了單元格,所以我無法識別信息源(工作表-工作簿)

Sub merge()
Application.DisplayAlerts = False
For Each hoja In ActiveWorkbook.Sheets
If hoja.Name = "todas" Then hoja.Delete
Next
Sheets.Add before:=Sheets(1)
ActiveSheet.Name = "todas"
For x = 2 To Sheets.Count
Sheets(x).Select
Range("a1:o" & Range("a650000").End(xlUp).Row).Copy
Sheets("todas").Range("a650000").End(xlUp).Offset(1, 0).PasteSpecial 
Paste:=xlValues
Next
Sheets("todas").Select
End Sub     

這是我必須合並的庫之一:

在此處輸入圖片說明

我沒有您的工作簿,所以我自己無法測試它,但是結構在那里,因此如果遇到錯誤,可以輕松調試它:

Sub merge()
    Dim rng As Range
    Dim cell As Range
    Application.DisplayAlerts = False
    For Each hoja In ActiveWorkbook.Sheets
    If hoja.Name = "todas" Then hoja.Delete
    Next
    Sheets.Add before:=Sheets(1)
    ActiveSheet.Name = "todas"

    For x = 2 To Sheets.Count
        Set rng = Sheets(x).UsedRange
        rng.Copy

        'Cell in column A after the last row
        Set cell = Sheets("todas").Range("a650000").End(xlUp).Offset(1, 0)
        cell.PasteSpecial Paste:=xlValues

        'Define the range that just got pasted (only column A)
        Set rng = cell.Resize(rng.Rows.Count, 1)

        'Offset it to the column next to the last column
        Set rng = rng.Offset(0, rng.Columns.Count)

        rng.Value = Sheets(x).Name 'paste the name ofthe sheet in each row
        Set rng = rng.Offset(0, 1)
        rng.Value = Sheets(x).Parent.Name 'paste the name of the workbook in each row

    Next
    Sheets("todas").Select
    Application.DisplayAlerts = True
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM