+ Reply to Thread
Results 1 to 11 of 11

Smart Merging

Hybrid View

TonyforVBA Smart Merging 10-07-2011, 05:40 AM
MickG Re: Smart Merging 10-07-2011, 06:17 AM
snb Re: Smart Merging 10-07-2011, 06:37 AM
Marcol Re: Smart Merging 10-07-2011, 07:05 AM
TonyforVBA Re: Smart Merging 10-07-2011, 07:19 AM
Marcol Re: Smart Merging 10-07-2011, 07:31 AM
royUK Re: Smart Merging 10-07-2011, 07:35 AM
TonyforVBA Re: Smart Merging 10-07-2011, 10:26 AM
TonyforVBA Re: Smart Merging 10-07-2011, 10:36 AM
Marcol Re: Smart Merging 10-07-2011, 10:59 AM
TonyforVBA Re: Smart Merging 10-07-2011, 11:47 AM
  1. #1
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Smile Smart Merging

    I'm trying to right a piece of code that merge the contents of 'column B'
    were the contents of 'column A' is repeated.

    I've attached my code below but unfortunately the Offset by one action does not work for me as there can be varios numbers of cells with the sage info.

    I've attached an example dats/result set.

    Sub Test()
    Dim Cell As Range
    For Each Cell In ActiveSheet.UsedRange
    'Horizontal
        'If Cell = Cell.Offset(0, 1) And Cell <> "" Then
         '   Cell.Offset(0, 1).ClearContents
          '  Range(Cell, Cell.Offset(0, 1)).Merge
        'End If
    'Vertical
        If Cell = Cell.Offset(1, 0) And Cell <> "" Then
            Cell.Offset(1, 0).ClearContents
            Range(Cell, Cell.Offset(1, 0)).Merge
        End If
        
    Next Cell
    End Sub
    Attached Files Attached Files
    Last edited by TonyforVBA; 10-07-2011 at 11:48 AM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Smart Merging

    Try this:-
    Sub MG07Oct22
    Dim Rng As Range
    Dim Dn As Range
    Dim K
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
        For Each Dn In Rng
            If Not .Exists(Dn.Value) Then
                .Add Dn.Value, Dn
            Else
                Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
            End If
    Next
    For Each K In .keys
        If .Item(K).Count > 1 Then
            .Item(K).Offset(, 1).Merge
        End If
     Next K
    End With
    
    End Sub
    Regards Mick

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Smart Merging

    If you want to use VBA you should refrain from merging cells (VBA doesn't like them).

    An alternative

    Sub snb()
     sn = Cells(1, 2).CurrentRegion
        
     For j = UBound(sn) To 2 Step -1
      If sn(j, 1) = sn(j - 1, 1) Then sn(j, 1) = ""
     Next
        
     Cells(1, 3).CurrentRegion.Offset(, 7) = sn
    End Sub
    PS Unfortunately I wasn't able to detect any 'sage' info in the file
    Last edited by snb; 10-07-2011 at 06:39 AM.



  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Smart Merging

    I agree, in this instance with snb, there is no such thing as "smart merging" of cells.

    However this would appear to do as you ask
    Option Explicit
    
    Sub MergeCells()
        Dim LastRow As Long, RowNo As Long, n As Long
    
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
        For RowNo = 3 To LastRow
            n = WorksheetFunction.CountIf(Range("A:A"), Cells(RowNo, "A"))
            With Range(Cells(RowNo, "B"), Cells(RowNo + n - 1, "B"))
                .ClearContents
                .Merge
            End With
            RowNo = RowNo + n - 1
        Next
    
    End Sub
    Your sample sheet seems to have data validation lists in Column B.
    Have a look at "Sheet1 (2)", does the formula solution do what you are after?
    (Just a guess.)
    Attached Files Attached Files
    Last edited by Marcol; 10-07-2011 at 07:10 AM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Smart Merging

    Thanks for you help with that lads. I've got what I neede to get done. Saved me alot of hassle.
    Cheers

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Smart Merging

    Are you going to let us into the secret? What saved you a lot of hassle.

    Note
    You have started 57 threads since you joined the forum, and only one has been marked [SOLVED]. Are all the others unsatisfactory answers?

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Smart Merging

    The only smart merging is no merging!!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  8. #8
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Smart Merging

    Hey guys, Sorry I wasnt aware we had to apply a solved status to the threads ourselves I taught it was taken care of my the forum moderators. I'll be sure to do this future.

    HOwever . . . I have run into abit of a snag! altough the below code has worked in the tests I carried out it does not appear to work on the spreadsheet I need it for. No error messages, it just merges the wrong cells by the look of things. Would one of you guys mind taking a look, I may be missing somthing quite simple here.

    I've attached a copy of the spreadsheet Im working on, with irrelevant content removed.
    Cheers

    
    Option Explicit
    
    Sub MergeCells()
        Dim LastRow As Long, RowNo As Long, n As Long
    
        LastRow = Range("F" & Rows.Count).End(xlUp).Row
    
        For RowNo = 3 To LastRow
            n = WorksheetFunction.CountIf(Range("F:F"), Cells(RowNo, "F"))
            With Range(Cells(RowNo, "X"), Cells(RowNo + n - 1, "X"))
                '.ClearContents
                .Merge
            End With
            RowNo = RowNo + n - 1
        Next
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Smart Merging

    Sorry, just realised I forgot to attach the file.
    Attached Files Attached Files

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Smart Merging

    Your data in column "F" must be sorted for this to work.

    If you can't sort the data for any reason, I suggest you forget about merging cells to get the end result you are looking for, it will cause you all sorts of trouble later.

    Merging Cells in Data Tables is a practice to be strongly discouraged.

  11. #11
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Smart Merging

    Thanks Marcol, I actually copped this shortly after putting up the post.
    Thanks for you help on this. I'll be sure to mark this as solved.

    Enjoy your weekend!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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