简体   繁体   English

合并确定工作表和工作簿源VBA的Excel工作表和工作簿

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

I have multiple workbooks and worksheets with the same information, I've been trying to merge all this files identifying the information source (Worksheet - workbook). 我有多个具有相同信息的工作簿和工作表,我一直试图合并所有标识信息源的文件(工作表-工作簿)。

I've used this code but it just merge the cells and I could not identify the information source (worksheet - Workbook) 我已经使用了这段代码,但是它只是合并了单元格,所以我无法识别信息源(工作表-工作簿)

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     

This is one of the libraries I have to merge: 这是我必须合并的库之一:

在此处输入图片说明

I did not have your workbook so I could not test it myself, but the structure is there so you can debug it easily if you ran into an error: 我没有您的工作簿,所以我自己无法测试它,但是结构在那里,因此如果遇到错误,可以轻松调试它:

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.

相关问题 Excel VBA将多个工作簿中的多个工作表合并到一个工作簿中 - excel vba merge multiple sheets from multiple workbooks into one workbook 将来自不同工作簿(在 1 个文件中)的几张工作表合并到包含 1 张工作表“sheets1”的工作簿 - merge several sheets from different workbooks (in 1 file) to workbook which contain 1 sheet "sheets1" 将3个工作簿合并到1个主工作簿Excel中 - Merge 3 Workbooks into 1 master workbook Excel 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python VBA - 根据汇总 Excel 表格上的条件,将工作簿中的不同模板工作表复制到另一个工作簿的多张工作表中 - VBA - copy different template sheets from a workbook, into multiple sheets of another workbook based on criteria on a summary excel sheet Excel VBA-打开另一个工作簿并检查工作表是否存在并合并 - Excel VBA - Open another workbook and check if sheet exist and Merge 将多个工作簿中的工作表合并到一个工作表中 - Merge sheets from multiple workbooks into one sheet Excel VBA:将多个工作簿合并为一个工作簿 - Excel VBA: Combine multiple workbooks into one workbook 试图将特定范围从工作簿中的许多工作表复制到另一个工作簿中的一个工作表 vba excel? - Trying to copy specific range from many sheets in workbook to one sheet in another workbook vba excel? 从各种工作簿中复制工作表并粘贴到其他工作簿中的工作表中 - Copying sheets from various workbooks and pasting into a sheet in a different workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM