Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: join an merge cells in column A but at same time the same rows in columns B and C

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location

    join an merge cells in column A but at same time the same rows in columns B and C

    I started with follow code but i cannot change it so it would join and merge the two other columns.
    If you have any ideas, please pass them on.

    PHP Code:
    Sub JoinMerge()
    Dim arr()
    Dim LR As Longct As LongAs Long
    Dim Rng 
    As RangeAs Range
    Application
    .ScreenUpdating False
    LR 
    Range("A" Rows.Count).End(xlUp).Row
    ct 
    1
    Set Rng 
    Range("A2:A" LR)
    For 
    Each c In Rng
        
    If c.Value <> "" Then
            ReDim Preserve arr
    (ct)
                
    arr(ct) = c.Row
            ct 
    ct 1
        End 
    If
    Next c
    For 1 To UBound(arr) - 1
    With Range
    (Cells(arr(t), "A"), Cells(arr(1) - 1"A"))
        .
    Merge
        
    .HorizontalAlignment xlCenter
        
    .VerticalAlignment xlCenter
    End With
    Next t
    With Range
    (Cells(arr(t), "A"), Cells(LR"A"))
        .
    Merge
        
    .HorizontalAlignment xlCenter
        
    .VerticalAlignment xlCenter
    End With
    Application
    .ScreenUpdating True
    End Sub 

  2. #2
    You'd better post your question in a forum that supports your native tongue: helpmij.nl

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    I do speak english at home. My wife is Canadian

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    .Resize(, 3)

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    Quote Originally Posted by mana View Post
    .Resize(, 3)
    I've tried to incorperate your proposal but didn't get the result i would like
    Therefor did i attached my test file so you can see what the data I'm working with
    Attached Files Attached Files

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    adding the desired output into a blank range or worksheet will help us understand your requirement.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location

    Angry

    Never thought of doing that. Thanks for the advice, mancubus
    Attached is the example with the desired output
    Attached Files Attached Files

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    instead of merging cells, i prefer merging cells' contents.

    Sub vbax_57403_join_merge_cells_in_3_cols()
    
        Dim NewTbl, RowNums
        Dim LR As Long, i As Long
        Dim JoinRange As Range, cll As Range
    
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count) 'working with a backup sheet
        With ActiveSheet
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            .Range("$A$1:$C$" & LR).AutoFilter Field:=3, Criteria1:="="
            .Range("$A$1:$C$" & LR).AutoFilter Field:=1, Criteria1:="="
            .Range("$A$1:$C$" & LR).Offset(1).SpecialCells(12).EntireRow.Delete 'delete blank rows based on Cols C and A
            .AutoFilterMode = False
            
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            UB1 = .Columns(1).SpecialCells(xlCellTypeConstants, 23).Count + 1
            
            ReDim RowNums(1 To UB1)
            For Each cll In .Columns(1).SpecialCells(xlCellTypeConstants, 23)
                i = i + 1
                RowNums(i) = cll.Row
            Next
            RowNums(UB1) = LR
            
            ReDim NewTbl(1 To UBound(RowNums) - 1, 1 To 3)
            For i = 1 To UBound(RowNums) - 1
                If RowNums(i + 1) - RowNums(i) = 1 Then
                    NewTbl(i, 1) = .Range("A" & i)
                    NewTbl(i, 2) = .Range("B" & i)
                    NewTbl(i, 3) = .Range("C" & i)
                Else
                    Set JoinRange = .Range("A" & RowNums(i) & ":A" & RowNums(i + 1) - 1)
                    NewTbl(i, 1) = Join(Application.Transpose(JoinRange.Value), Chr(10))
                    Set JoinRange = .Range("B" & RowNums(i) & ":B" & RowNums(i + 1) - 1)
                    NewTbl(i, 2) = Join(Application.Transpose(JoinRange.Value), Chr(10))
                    Set JoinRange = .Range("C" & RowNums(i) & ":C" & RowNums(i + 1) - 1)
                    NewTbl(i, 3) = Join(Application.Transpose(JoinRange.Value), Chr(10))
                End If
            Next i
            
            .Cells.Clear
            .Cells(1).Resize(UBound(NewTbl, 1), UBound(NewTbl, 2)) = NewTbl
        End With
    
        With Application
            .EnableEvents = True
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    function for only you

    Option Explicit
    
    Function MergeSpecial(rr As Range)
        Dim s As String
        Dim r As Range
        
        For Each r In rr
            s = s & vbLf & r.Value
        Next
        rr.Value = Empty
        rr.Merge
        rr.Value = Mid(s, 2)
    
    End Function
    then, insert the function in your original code.

    With Range(Cells(arr(t), "A"), Cells(arr(t + 1) - 1, "A"))
        Call MergeSpecial(.Offset(, 1))
        Call MergeSpecial(.Offset(, 2))
        .Merge

  10. #10
    Hou het simpel:

    Sub M_snb()
       [a1:C36].Replace vbLf, ""
       [a1:C36].Replace vbCr, ""
       
       sn = [a1:C36]
       ReDim sp(1 To Columns(1).SpecialCells(2).Count, 1 To 3)
       
       For j = 1 To UBound(sn)
          If sn(j, 1) <> "" Then jj = jj + 1
          If sn(j, 1) <> "" Then sp(jj, 1) = sn(j, 1)
          If sn(j, 2) <> "" Then sp(jj, 2) = sp(jj, 2) & IIf(sp(jj, 2) = "", "", vbLf) & sn(j, 2)
          If sn(j, 3) <> "" Then sp(jj, 3) = sp(jj, 3) & IIf(sp(jj, 3) = "", "", vbLf) & sn(j, 3)
       Next
    
       Cells(1, 12).Resize(UBound(sp), UBound(sp, 2)) = sp
    End Sub

  11. #11
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    Mancubus,
    the code does the work with a low amount of letters in the cells but when there's a lot of letters, the code gives "Run-time error '13': Type mismatch"
    Is there a solutions for this error?

  12. #12
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    snb,

    How do I declare sp, sn, j and jj

  13. #13
    You don't have to: remove 'option explicit'.

  14. #14
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    snb
    er verandert niets

  15. #15
    Kijk eens in kolom L, M en N

  16. #16
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    snb
    Inderdaad daar is de oplossing die ik voor ogen had

    Hartelijk bedankt

  17. #17
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by acraens View Post
    Mancubus,
    the code does the work with a low amount of letters in the cells but when there's a lot of letters, the code gives "Run-time error '13': Type mismatch"
    Is there a solutions for this error?
    just not to leave the question unreplied:
    https://msdn.microsoft.com/en-us/lib.../gg251467.aspx
    A variable-length string can contain up to approximately 2 billion (2^31) characters.
    A fixed-length string can contain 1 to approximately 64K (2^16) characters.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  18. #18
    VBAX Regular
    Joined
    Oct 2010
    Location
    Leuven, Belgium
    Posts
    13
    Location
    mancubus,
    thanks for the explanation.
    I'll keep looking what the problem is in the data I'm trying to manipulate.

    Thank you for all your help

  19. #19
    I'd prefer:
    Sub M_snb()
        [a1:C36].Replace vbLf, ""
        [a1:C36].Replace vbCr, ""
         
        sn = [a1:C36]
        ReDim sp(1 To Columns(1).SpecialCells(2).Count, 1 To 3)
         
        For j = 1 To UBound(sn)
            If sn(j, 1) <> "" Then y = y + 1
            For jj = 1 To 3
                sp(y, jj) = sp(y, jj) & IIf(sp(y, jj) = "" Or sn(j, jj) = "", "", vbLf) & sn(j, jj)
            Next
        Next
         
        Cells(1, 12).Resize(UBound(sp), UBound(sp, 2)) = sp
    End Sub

  20. #20
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    correction:

    change
            RowNums(UB1) = LR
    to
            RowNums(UB1) = LR + 1
    in post #8 to include the last row value in column C
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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