简体   繁体   English

如何将数据从多个工作表的一个Excel文件复制到多个工作表的另一个Excel文件中

[英]How do I copy the data from one Excel file of multiple sheets to another Excel file of multiple sheets

I have one excel file of multiple sheets with column names and values and sheetnames. 我有一个包含多个工作表的Excel文件,其中包含列名,值和工作表名。
I have another excel file of multiple sheets with column names and sheet names. 我还有另一个具有列名称和工作表名称的多个工作表的excel文件。
I want to copy the data (column values) from one excel to another excel without changing the sheetnames, since the sheetnames are different, but the column names are same. 我想在不更改工作表名称的情况下将数据(列值)从一个Excel复制到另一个Excel,因为工作表名称不同,但是列名称相同。
Pleased to hear some suggestions. 很高兴听到一些建议。

My suggestion is to define functions in separate modules but here I defined 2 functions in the main module (activating of workbook and worksheet) for simplicity. 我的建议是在单独的模块中定义功能,但在这里我为简单起见在主模块中定义了2个功能(激活工作簿和工作表)。 The macro is on separate excel file (Macro.xlsm). 该宏位于单独的Excel文件(Macro.xlsm)中。 There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location. 同一位置中包含两个Excel文件(Book1.xlsx和Book2.xlsx)。

在此处输入图片说明

I tried to answer generally with this example so it can be extended for many workbooks and worksheets. 我试图用这个例子来回答,所以可以扩展到许多工作簿和工作表。

在此处输入图片说明

Book2.xlsx before running the macro. 运行宏之前,先执行Book2.xlsx。

在此处输入图片说明

Book2.xlsx after running the macro. 运行宏后,Book2.xlsx。 The destination row was selected one row lower intentionally :-) 目的行被故意选择低了一行:-)

在此处输入图片说明

Option Explicit

Dim wb01 As Workbook, wb02 As Workbook
Public paTh01 As Variant, paTh02 As Variant


'/Define your functions
'Function1 openBook(paTh0, wB0)
Function openBook(path0 As Variant, wB0 As Workbook)
        Set wB0 = Workbooks.Open(path0)
        wB0.Activate
End Function

'Function2 openSheet(wB0, "Sheet_Name")
Function openSheet(wB0 As Workbook, sheetName0 As String)
        wB0.Activate
        Sheets(sheetName0).Activate
End Function

'Main Module
Sub main()

    paTh01 = "D:\Book1.xlsx"
    paTh02 = "D:\Book2.xlsx"

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    Call openBook(paTh01, wb01)
    Call openSheet(wb01, "mySheet1")
    Range("A2:D4").Select
    With Selection
        .Orientation = 0
        .Copy
    End With

    'If you have a loop, you should put delay otherwise excel will crash
    Application.Wait (Now + TimeValue("0:00:01"))

    Call openBook(paTh02, wb02)
    Call openSheet(wb02, "mySheet2")
    Range("A3:D5").PasteSpecial xlPasteValues, Transpose:=False


    wb01.Close savechanges:=False
    DoEvents

    wb02.Close savechanges:=True
    DoEvents

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过pandas将多个工作表中的列合并到一个excel文件中 - How to merge columns from multiple sheets in one excel file by pandas 如何让python一张一张地读取一个excel文件上的多张纸 - How to make python read multiple sheets on an excel file one by one 如何将 DataFrame 导出到多张 Excel 文件 - How to export a DataFrame to multiple sheets of Excel File 如何在 pandas 中打开一个 excel 文件? - How to open an excel file with multiple sheets in pandas? 如何从 python 读取多张 excel 文件? 我收到错误消息说“pandas”没有属性“excel” - How to read excel file with multiple sheets from python? I got error saying 'pandas' has no attribute 'excel' 如何将字典(从导入的 Excel 文件/表格)转换为多个数据帧? - How to convert dictionary (from imported Excel file/sheets) into multiple dataframes? 如何连接同一个文件中的多个excel表? - how to concatenate multiple excel sheets from the same file? Python:如何从一个 excel 文件中循环遍历多个工作表并将它们组合成一个 dataframe - Python: How to loop through multiple sheets from one excel file and combine them into one dataframe 将多个数据框写入 Excel 文件中的多个工作表 - Writing multiple dataframes to multiple sheets in an Excel file 如何在不发生此错误的情况下将多个数据框保存到一个 Excel 文件(作为单独的工作表)中? - How can I save multiple dataframes onto one excel file (as separate sheets) without this error occurring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM