+ Reply to Thread
Results 1 to 11 of 11

Gather similar data in two tables

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Gather similar data in two tables

    Hello everyone

    In Sheet1 I have two tables and I need to compare these two tables in another sheet
    The logic will be as following:
    =====================
    Look at the value in column A and if found in Column F then grab the same value in both columns to the same row and so on
    As for these similar rows in both tables would be highlighted ...

    After all there will be different values in both tables (these will be put beneath each table and sorted in ascending way)

    I have put the expected output in Sheets("Output") to give you the final desired result)

    Hope it is clear
    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Gather similar data in two tables

    Try this. I'm assuming there is only one match for each in column A and F

    Sub grouplikes()
    
    Dim lastrow, lastrowa, rownum, currentrow, i As Single
    lastrowa = Application.CountA(Range("A:A"))
    lastrow = Application.CountA(Range("D:D"))
    rownum = 3
    currentrow = 3
    Do While Range("A" & rownum).Value <> ""
        For i = 3 To lastrow
        If Range("A" & rownum).Value = Range("F" & i).Value Then
            Range("A" & rownum & ":D" & rownum).Copy
            ActiveSheet.Paste Destination:=Worksheets("Output").Range("A" & currentrow)
             Range("A" & rownum & ":D" & rownum).Cut
             ActiveSheet.Paste Destination:=Worksheets("Output").Range("F" & currentrow)
            currentrow = currentrow + 1
            Sheets("Sheet1").Range("F" & i & ":I" & i).ClearContents
            i = lastrow
        End If
        Next i
        rownum = rownum + 1
    Loop
    
    Sheets("output").Range("A3:D" & currentrow - 1).Interior.ThemeColor = xlThemeColorAccent5
    Sheets("output").Range("F3:I" & currentrow - 1).Interior.ThemeColor = xlThemeColorAccent5
        
    Range("A3:D" & lastrowa).Cut
    ActiveSheet.Paste Destination:=Worksheets("Output").Range("A" & currentrow)
        
        ActiveWorkbook.Worksheets("Output").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Output").Sort.SortFields.Add Key:=Range("A" & currentrow & ":A" & lastrowa + currentrow) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Output").Sort
            .SetRange Range("A" & currentrow & ":D" & lastrowa + currentrow)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    Range("F3:I" & lastrowa).Cut
    ActiveSheet.Paste Destination:=Worksheets("Output").Range("F" & currentrow)
        
        ActiveWorkbook.Worksheets("Output").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Output").Sort.SortFields.Add Key:=Range("F" & currentrow & ":F" & lastrow + currentrow) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Output").Sort
            .SetRange Range("F" & currentrow & ":I" & lastrow + currentrow)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    
    End Sub
    Happy with my advice? Click on the * reputation button below

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Gather similar data in two tables

    Thanks a lot for reply and for working solution.. But I need to keep original data as it is (original data cleared)

    Is it possible to do this task using VBA arrays or dictionary?
    Last edited by YasserKhalil; 09-20-2016 at 06:54 AM.

  4. #4
    Registered User
    Join Date
    05-21-2006
    Posts
    5

    Re: Gather similar data in two tables


    اخي ياسر

    انجليزيتي ضعيفه

    هل يمكنك ارسال المطلوب علي الخاص في اوفسينا


  5. #5
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Gather similar data in two tables

    If it is possible to do with arrays or dictionaries I'm not aware how.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Gather similar data in two tables

    Maybe :
    Sub Test()
      Dim coll As New Collection, arr(), i As Long, j As Long, strKey As String
      With Sheets("Sheet1")
        i = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(.Rows.Count, "F").End(xlUp).Row)
        arr = .Range("A3:J" & i).Value
      End With
      For i = 1 To UBound(arr, 1)
          For j = 1 To UBound(arr, 2) Step 5
              If Len(arr(i, j)) Then
                 On Error Resume Next
                    coll.Add key:=arr(i, j), Item:=New Collection
                 On Error GoTo 0
                 coll(arr(i, j)).Add Empty
              End If
          Next j
      Next i
      For i = 1 To UBound(arr, 1)
          For j = 1 To UBound(arr, 2) Step 5
              If Len(arr(i, j)) Then
                 arr(i, j + 4) = IIf(coll(arr(i, j)).Count = 2, 1, "")
              End If
          Next j
      Next i
      With Sheets("Output")
        .Cells.Clear
        Sheets("Sheet1").Rows(2).Copy .Rows(2)
        With .Range("A3").Resize(UBound(arr, 1), UBound(arr, 2))
          .Value = arr
          .Columns("A:E").Sort key1:=.Columns("E"), order1:=xlAscending, key2:=.Columns("A"), order2:=xlAscending, header:=xlNo
          .Columns("F:J").Sort key1:=.Columns("J"), order1:=xlAscending, key2:=.Columns("F"), order2:=xlAscending, header:=xlNo
          Intersect(.Areas(1), .Columns("E").SpecialCells(xlCellTypeConstants).EntireRow).Interior.Color = 16777164
          .Columns("E").Clear
          .Columns("J").Clear
        End With
      End With
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Gather similar data in two tables

    Thanks a lot Mr. Omar
    Mr. Karedog thank you very much for this awesome solution. You know I am admired of VBA arrays

    Just a little point : I don't need to sort similar data .. The sort process will be executed just for the different in the two tables
    But as for the similar I need the same order as they were in Sheet1 Column A
    Best Regards

  8. #8
    Registered User
    Join Date
    05-21-2006
    Posts
    5

    Re: Gather similar data in two tables

    I am struggling here:

    مع تحيات منتدي اوفسينا
    Attached Files Attached Files

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Gather similar data in two tables

    Maybe like this ?
    Sub Test()
      Dim coll As New Collection, arr(), c As Long, i As Long, j As Long, strKey As String
      With Sheets("Sheet1")
        i = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(.Rows.Count, "F").End(xlUp).Row)
        arr = .Range("A3:J" & i).Value
      End With
      For j = 1 To UBound(arr, 2) Step 5
          For i = 1 To UBound(arr, 1)
              If Len(arr(i, j)) Then
                 On Error Resume Next
                    coll.Add key:=arr(i, j), Item:=New Collection
                 On Error GoTo 0
                 coll(arr(i, j)).Add Empty
              End If
          Next i
      Next j
      For j = 1 To UBound(arr, 2) Step 5
          For i = 1 To UBound(arr, 1)
              If Len(arr(i, j)) Then
                 With coll(arr(i, j))
                   If .Count = 2 Then
                      c = c + 1
                      arr(i, j + 4) = Val("1." & Format$(c, "00000"))
                      .Add arr(i, j + 4)
                   ElseIf .Count = 3 Then
                      arr(i, j + 4) = .Item(.Count)
                   End If
                 End With
              End If
          Next i
      Next j
      With Sheets("Output")
        .Cells.Clear
        Sheets("Sheet1").Rows(2).Copy .Rows(2)
        With .Range("A3").Resize(UBound(arr, 1), UBound(arr, 2))
          .Value = arr
          .Columns("A:E").Sort key1:=.Columns("E"), order1:=xlAscending, key2:=.Columns("A"), order2:=xlAscending, header:=xlNo
          .Columns("F:J").Sort key1:=.Columns("J"), order1:=xlAscending, key2:=.Columns("F"), order2:=xlAscending, header:=xlNo
          Intersect(.Areas(1), .Columns("E").SpecialCells(xlCellTypeConstants).EntireRow).Interior.Color = 16777164
          .Columns("E").Clear
          .Columns("J").Clear
        End With
      End With
    End Sub

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Gather similar data in two tables

    That's really awesome my dear friend.. Thanks a lot for great and wonderful help
    Kind regards

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Gather similar data in two tables

    You are welcome, thanks for marking the thread as solved and for the rep.points.


    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Gather data for similar group
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2016, 05:13 AM
  2. Matching Data with 3 Similar Tables(level)
    By henryyeo in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-18-2014, 05:57 AM
  3. Gather data across several similar sheets
    By fgruhlke in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-04-2014, 10:16 PM
  4. [SOLVED] Create 10 similar tables with Excel VBA
    By kaligad in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-14-2013, 01:54 PM
  5. Replies: 2
    Last Post: 04-29-2010, 04:27 AM
  6. Creating multiple pivot tables for similar data
    By shockeroo in forum Excel General
    Replies: 2
    Last Post: 03-31-2010, 11:05 AM
  7. Comparing 2 tables with similar content
    By Vic1978 in forum Excel General
    Replies: 5
    Last Post: 12-05-2005, 04:10 PM

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