+ Reply to Thread
Results 1 to 6 of 6

merging two column with VBA

Hybrid View

orhanceliloglu merging two column with VBA 03-01-2018, 11:38 AM
LeoTaxi Re: merging two column with... 03-01-2018, 08:44 PM
orhanceliloglu Re: merging two column with... 03-02-2018, 04:17 AM
daboho Re: merging two column with... 03-02-2018, 10:17 AM
LeoTaxi Re: merging two column with... 03-02-2018, 04:42 AM
orhanceliloglu Re: merging two column with... 03-02-2018, 05:37 AM
  1. #1
    Forum Contributor
    Join Date
    11-01-2016
    Location
    Prague
    MS-Off Ver
    2013
    Posts
    121

    Red face merging two column with VBA

    Hi Everyone,

    I need a simple merging code for two tables. But although I checked the forum for 2 hours I couldnt find anything. Since the solution is short thing (I believe), I dont have a code right now sorry.

    I attached my excel. So there are 3 columns A B C - and E F G

    They have same type of data. The thing i am trying, to merge them into an another 3 column, eg. I J K

    It shouldnt delete the duplicates, since I need them. Maybe highlighting the duplicates would be amazing.

    If you can just show me a way, I can do the rest.

    Many thanks !
    Orhan
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: merging two column with VBA

    Sub test()
    Dim mr As Range, i As Long, lr1 As Long, lr2 As Long, lr3 As Long, arr1, arr2
    lr1 = Range("A" & Rows.Count).End(xlUp).Row
    lr2 = Range("E" & Rows.Count).End(xlUp).Row
    lr3 = lr1 + lr2 - 1
    arr1 = Range("A2", "D" & lr1)
    arr2 = Range("E2", "H" & lr2)
    For i = 1 To UBound(arr1)
        arr1(i, 4) = arr1(i, 1) & arr1(i, 2) & arr1(i, 3)
    Next
    For i = 1 To UBound(arr2)
        arr2(i, 4) = arr2(i, 1) & arr2(i, 2) & arr2(i, 3)
    Next
    Range("I2").Resize(UBound(arr1), UBound(arr1, 2)) = arr1
    Range("I" & lr1 + 1).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
    Range("M2", "M" & lr3).FormulaR1C1 = "=countif(C[-1],RC[-1])"
    Range("I1", "M" & lr3).AutoFilter 5, Criteria1:=">1"
    Set mr = Range("I2", "M" & lr3).SpecialCells(12)
    mr.Interior.Color = vbYellow
    ActiveSheet.AutoFilterMode = False
    Range("L:M").Clear
    Range("I:K").Columns.AutoFit
    End Sub
    Kind regards
    Leo

  3. #3
    Forum Contributor
    Join Date
    11-01-2016
    Location
    Prague
    MS-Off Ver
    2013
    Posts
    121

    Re: merging two column with VBA

    Thank you very much Leo, That looks amazing !

    Only one thing, you highlighted if there is a same ID and same reason together. But actually, I need to see if there is a same ID even if the reasons are different.

    May I kindly ask to adjust the count if for only IDs which is the I column.

    maaaany thanks !
    Orhan

  4. #4
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: merging two column with VBA

    Sub daboho2()
    Dim a, b, c, x, i As Long, temp$, r As Long, j As Long
    a = Range("A2", [C30000].End(3)): b = Range("E2", [G30000])
    c = Array(a, b)
    For i = 0 To UBound(c)
         Range("I8000").End(3).Offset(1).Resize(UBound(c(i), 1), UBound(c(i), 2)).Value = c(i)
    Next
    Dim d As Object: Set d = CreateObject("scripting.dictionary")
    With [i2].CurrentRegion: x = .Value
    On Error Resume Next
      For i = 1 To UBound(x)
      temp = x(i, 1) & x(i, 3)
        If Not d.exists(temp) Then
         j = j + 1
         d.Add temp, j
                .Cells(i, 1).Resize(, 3).Interior.ColorIndex = j
        Else
                .Cells(i, 1).Resize(, 3).ColorIndexindex = d(temp)
        End If
      Next i
    End With
    End Sub
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  5. #5
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: merging two column with VBA

    then looks like this

    Sub test()
    Dim mr As Range, i As Long, lr1 As Long, lr2 As Long, lr3 As Long, arr1, arr2
    lr1 = Range("A" & Rows.Count).End(xlUp).Row
    lr2 = Range("E" & Rows.Count).End(xlUp).Row
    lr3 = lr1 + lr2 - 1
    arr1 = Range("A2", "C" & lr1)
    arr2 = Range("E2", "G" & lr2)
    Range("I2").Resize(UBound(arr1), UBound(arr1, 2)) = arr1
    Range("I" & lr1 + 1).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
    Range("L2", "L" & lr3).FormulaR1C1 = "=countif(C[-3],RC[-3])"
    Range("I1", "L" & lr3).AutoFilter 4, Criteria1:=">1"
    Set mr = Range("I2", "L" & lr3).SpecialCells(12)
    mr.Interior.Color = vbYellow
    ActiveSheet.AutoFilterMode = False
    Range("L:L").Clear
    Range("I:K").Columns.AutoFit
    End Sub
    Kind regards
    Leo

  6. #6
    Forum Contributor
    Join Date
    11-01-2016
    Location
    Prague
    MS-Off Ver
    2013
    Posts
    121

    Re: merging two column with VBA

    Worked perfect. Thank you very much Leo !!

+ 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. Merging cells in a column to represent the value of another column
    By tabascorez in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-15-2017, 02:06 AM
  2. Merging Multiple Sheet's column of data based on other column's cell value
    By BALA1983 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2017, 12:32 AM
  3. Merging some data from Column A, some from Column B, and all of Column C--a fun one :)
    By richardwedamins in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-18-2014, 11:04 AM
  4. Merging column data
    By expert40 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-03-2013, 03:37 PM
  5. Replies: 1
    Last Post: 01-11-2013, 06:26 PM
  6. merging 4 columns into 1 new column
    By abacaxi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2007, 05:18 PM
  7. Column Merging
    By Scott McKinney in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-19-2006, 02:25 AM

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