Results 1 to 13 of 13

[SOLVED] VBA combine data and remove duplicates

Threaded View

  1. #1
    Registered User
    Join Date
    10-06-2023
    Location
    Washington DC, USA
    MS-Off Ver
    365, version 2307
    Posts
    11

    [SOLVED] VBA combine data and remove duplicates

    Hello, I need help!
    I work in Excel all the time but am not a specialist in VBA.

    What is the goal - to have a macro that would combine data from two or more separate Excel files into a new file, pasting data one set after another. And then remove duplicates from the combined spreadsheet. All datasets have an equal number of columns but a different number of rows. This macro I want to save into my colleagues Excel personal.xlsb files so that they could perform these operations routinely. Most of them are not really good with Excel so the macro should ease the process for them.

    So I found the code below for combining data. It works fine if I save this macro within a workbook where the datasets will be combined. However, if I save this macro to the personal.xlsb it combines data in that personal.xlsb. Unfortunately, I am rather ignorant in VBA and don't know how to fix it. The idea is that a person opens a new workbook, clicks the macro shortcut, the is oofered to choose files to combine data and data is combined on a spreadsheet in that new workbook. Please help and correct the coding.

    Then I myself recorded a macro to remove duplicate rows. But then when I apply it to a new data set which can be larger, it doesn't work. Would you help correcting it so it can work with any number of rows (and maybe any number of columns), please?

    At the end I plan to combine the macros and run it as one.

    First code (CombineData)

    Sub CombineData()
    'Declare variables
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim fd As FileDialog
    Dim file As Variant
    Dim rng As Range
    Dim LastRow As Long
    Dim LastCol As Long

    'Create a new sheet for the combined data
    Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = "Combined Data"

    'Create a file dialog to select the files to combine
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.Title = "Select the files to combine"

    'Show the file dialog and get the selected files
    If fd.Show = -1 Then
    For Each file In fd.SelectedItems
    'Open the selected file as read-only
    Set wb = Workbooks.Open(file, ReadOnly:=True)
    'Get the used range of the first sheet
    Set rng = wb.Sheets(1).UsedRange
    'Get the last row and column of the combined data sheet
    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    'If the combined data sheet is empty, copy the headers and data from the first file
    If LastRow = 1 And LastCol = 1 And ws.Cells(1, 1) = "" Then
    rng.Copy ws.Cells(1, 1)
    Else
    'Otherwise, copy only the data (without headers) from the other files and append it to the combined data sheet
    rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count).Copy ws.Cells(LastRow + 1, 1)
    End If

    'Close the selected file without saving changes
    wb.Close SaveChanges:=False
    Next file

    'Inform the user that the data has been combined successfully
    MsgBox "The data has been combined successfully in the sheet ""Combined Data"".", vbInformation, "Done"

    Else
    'If no files are selected, inform the user and exit the sub
    MsgBox "No files were selected. Please try again.", vbExclamation, "Canceled"
    Exit Sub
    End If

    End Sub



    Second code (RemoveDuplicates)

    Sub RemoveDuplicates()
    Cells.Select
    ActiveSheet.Range("$A$1:$V$5907").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
    , 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22), Header:=xlYes
    End Sub


    Thank you!!!
    Attached Files Attached Files
    Last edited by andreys; 10-09-2023 at 10:52 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Combine data from multiple sheets and remove duplicates if any
    By paradise2sr in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 08-21-2022, 09:25 AM
  2. Combine data through different worksheets and remove duplicates
    By Haree in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2020, 10:27 AM
  3. [SOLVED] Combine values from columns with remove duplicates
    By wrybel in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-20-2019, 06:41 AM
  4. Replies: 6
    Last Post: 10-19-2018, 02:44 PM
  5. [SOLVED] Combine cells and remove duplicates
    By deanblew in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-27-2016, 10:09 AM
  6. Replies: 4
    Last Post: 01-24-2016, 10:40 AM
  7. VBA to combine lists and remove duplicates in Excel 2010
    By pavaho in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-24-2014, 06:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1