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










0















I have one excel file of multiple sheets with column names and values and sheetnames.

I have another excel file of multiple sheets with column names and sheet names.

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.

Pleased to hear some suggestions.










share|improve this question






















  • What did you try already?

    – Dominique
    Nov 16 '18 at 10:09















0















I have one excel file of multiple sheets with column names and values and sheetnames.

I have another excel file of multiple sheets with column names and sheet names.

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.

Pleased to hear some suggestions.










share|improve this question






















  • What did you try already?

    – Dominique
    Nov 16 '18 at 10:09













0












0








0








I have one excel file of multiple sheets with column names and values and sheetnames.

I have another excel file of multiple sheets with column names and sheet names.

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.

Pleased to hear some suggestions.










share|improve this question














I have one excel file of multiple sheets with column names and values and sheetnames.

I have another excel file of multiple sheets with column names and sheet names.

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.

Pleased to hear some suggestions.







python excel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 9:57









aliali

679




679












  • What did you try already?

    – Dominique
    Nov 16 '18 at 10:09

















  • What did you try already?

    – Dominique
    Nov 16 '18 at 10:09
















What did you try already?

– Dominique
Nov 16 '18 at 10:09





What did you try already?

– Dominique
Nov 16 '18 at 10:09












1 Answer
1






active

oldest

votes


















1














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. The macro is on separate excel file (Macro.xlsm). There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location.



enter image description here



I tried to answer generally with this example so it can be extended for many workbooks and worksheets.



enter image description here



Book2.xlsx before running the macro.



enter image description here



Book2.xlsx after running the macro. The destination row was selected one row lower intentionally :-)



enter image description here



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





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335374%2fhow-do-i-copy-the-data-from-one-excel-file-of-multiple-sheets-to-another-excel-f%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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. The macro is on separate excel file (Macro.xlsm). There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location.



    enter image description here



    I tried to answer generally with this example so it can be extended for many workbooks and worksheets.



    enter image description here



    Book2.xlsx before running the macro.



    enter image description here



    Book2.xlsx after running the macro. The destination row was selected one row lower intentionally :-)



    enter image description here



    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





    share|improve this answer





























      1














      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. The macro is on separate excel file (Macro.xlsm). There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location.



      enter image description here



      I tried to answer generally with this example so it can be extended for many workbooks and worksheets.



      enter image description here



      Book2.xlsx before running the macro.



      enter image description here



      Book2.xlsx after running the macro. The destination row was selected one row lower intentionally :-)



      enter image description here



      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





      share|improve this answer



























        1












        1








        1







        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. The macro is on separate excel file (Macro.xlsm). There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location.



        enter image description here



        I tried to answer generally with this example so it can be extended for many workbooks and worksheets.



        enter image description here



        Book2.xlsx before running the macro.



        enter image description here



        Book2.xlsx after running the macro. The destination row was selected one row lower intentionally :-)



        enter image description here



        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





        share|improve this answer















        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. The macro is on separate excel file (Macro.xlsm). There are two excel files (Book1.xlsx & Book2.xlsx) included in the same location.



        enter image description here



        I tried to answer generally with this example so it can be extended for many workbooks and worksheets.



        enter image description here



        Book2.xlsx before running the macro.



        enter image description here



        Book2.xlsx after running the macro. The destination row was selected one row lower intentionally :-)



        enter image description here



        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 12:06

























        answered Nov 16 '18 at 11:35









        MOHAMMAD ALA AMJADIMOHAMMAD ALA AMJADI

        364




        364





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335374%2fhow-do-i-copy-the-data-from-one-excel-file-of-multiple-sheets-to-another-excel-f%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric