Home > Mobile >  VBA code to combine two Workbooks into one Worksheet
VBA code to combine two Workbooks into one Worksheet

Time:06-25

I want to know if it is possible to bring two workbooks (sheet1) into one worksheet (master workbook). I need to have two data together in one worksheet.

Any help is really appreciated. Below are screenshots for further details if helps.

Two files:

  1. Fundraise-pages(1).csv
  2. Supporters(1).csv

Please see the attached for further details if help?

enter image description here


Thank you for looking into this.

File 1 and File 2 data - both have the same email addresses.

Importantly I would like a macro to pull data to identify or even match for both email addresses. If found then add it onto a worksheet.

For example on the worksheet (master) there should be a data file 1 on the left hand side and the data file 2 on the right hand side including headings.

I hope you can see my images clearly?

Regards

V

enter image description here

enter image description here

CodePudding user response:

Here is an overview to start with:

https://support.microsoft.com/en-us/office/import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba

Vba Import CSV files to Excel

Proposal: Using the connection via PowerQuery allows you to further process them as tables and also join/merge them into one while you can see and follow each step of the process.

CodePudding user response:

Something like this should do what you want. Feel free to change the code to suit your needs.

Sub CopyRangeFromMultiWorksheets()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim CopyRng As Range

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Delete the sheet "RDBMergeSheet" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "RDBMergeSheet"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "RDBMergeSheet"

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> DestSh.Name Then

            'Find the last row with data on the DestSh
            Last = LastRow(DestSh)

            'Fill in the range that you want to copy
            Set CopyRng = sh.Range("A1:G1")

            'Test if there enough rows in the DestSh to copy all the data
            If Last   CopyRng.Rows.Count > DestSh.Rows.Count Then
                MsgBox "There are not enough rows in the Destsh"
                GoTo ExitTheSub
            End If

            'This example copies values/formats, if you only want to copy the
            'values or want to copy everything look at the example below this macro
            CopyRng.Copy
            With DestSh.Cells(Last   1, "A")
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            End With

            'Optional: This will copy the sheet name in the H column
            DestSh.Cells(Last   1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name

        End If
    Next

ExitTheSub:

    Application.Goto DestSh.Cells(1)

    'AutoFit the column width in the DestSh sheet
    DestSh.Columns.AutoFit

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Function LastRow(sh As Worksheet) On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 End Function

Function LastCol(sh As Worksheet) On Error Resume Next LastCol = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 End Function

Source code:

https://www.rondebruin.nl/win/s3/win002.htm

Also, check this out.

https://www.rondebruin.nl/win/addins/rdbmerge.htm

  • Related