-8

I want to merge cells in one row (belongs to Column B) with the count of already merged different cell(belongs to Column A) .How can i start coding ?

this is the screenshot that i want

this is the screenshot that i want

Ralph
  • 9,284
  • 4
  • 32
  • 42
Ylmz_eng
  • 1
  • 2
  • 2
    What have you tried? Start by using the Macro Recorder to do this manually. Then take a look at the code, and [remove the `.Select`](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros), and you should start getting there. As for counting how many merged cells, you'll want to look up the [various ways](http://stackoverflow.com/a/22076713/4650297) to do so. – BruceWayne Jun 09 '16 at 17:27
  • Welcome to SO. Please read [How to Ask]( http://stackoverflow.com/help/how-to-ask) to receive the most effective help here. Since it your first day, I do not wish to discourage you, I am extending help to you by providing some examples and related code to get you started. Please understand that SO is not a code for me service. You have to research on your topic. If you give the search term ~[excel-vba] Merge Cells~ in Questions section of SO, there are 721 results with a wealth of information covering variety of situations. Hope you put efforts and become an important contributing member. – skkakkar Jun 09 '16 at 19:46
  • Thanks for your advice. I will pay attention when asking other questions. – Ylmz_eng Jun 11 '16 at 11:28

1 Answers1

0
  1. Merging cells in a spreadsheet means taking two or more cells and constructing a single cell out of them. When you merge two or more adjacent horizontal or vertical cells, the cells become one larger cell that is displayed across multiple columns or rows. When you merge multiple cells, the contents of only one cell (the upper-left cell for left-to-right languages, or the upper-right cell for right-to-left languages) appear in the merged cell. The contents of the other cells that you merge are deleted. For more details please go through this MSDN article Merge and unmerge cells

Simple VBA code for Merging Cell

Sub merg_exp_1()
    ActiveSheet.Range("A1:C10").Merge
End Sub

Sample data before and after running the program is shown. Exhibit-1Exhibit-2

  1. Now let us see, If we merge a row what happens. Sample code for this exercise though general is being tested for one situation only and it as follow :

    Sub Merge_Rows()
        Dim rng As Range
        Dim rrow As Range
        Dim rCL As Range
        Dim out As String
        Dim dlmt As String
        dlmt = ","
        Set rng = ActiveSheet.Range("A1:C5")
        For Each rrow In rng.Rows
        out = ""
        For Each rCL In rrow.Cells
            If rCL.Value <> "" Then
                out = out & rCL.Value & dlmt
            End If
        Next rCL
        Application.DisplayAlerts = False
        rrow.Merge
        Application.DisplayAlerts = True
        If Len(rrow.Cells(1).Value) > 0 Then
            rrow.Cells(1).Value = Left(out, Len(out) - 1)
        End If
        Next rrow
    End Sub
    

Sample data before and after running the program is shown. You can see this won't meet your objective.

Snapshot-3Snapshot-4

  1. Next we can try merging by column approach. Here also we are trying for one column i.e. Column B to see the effect. Sample code as follows.

    Sub Merge_col_exp()
    
        Dim cnum As Integer
        Dim rng As Range
        Dim str As String
    
        For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
        cnum = Cells(i, 1).MergeArea.Count
        Set rng = Range(Cells(i, 2), Cells(i - cnum + 1, 2)) ' only to demonstrate working in 2nd column
    
        For Each cl In rng
        If Not IsEmpty(cl) Then str = str + "," + cl
        Next
        If str <> "" Then str = Right(str, Len(str) - 1)
    
        Application.DisplayAlerts = False
        rng.Merge
        rng = str
        Application.DisplayAlerts = True
    
        str = ""
        i = i - cnum + 1
        Next i
    
    End Sub
    

Snapshot-5Snapshot-6 Sample data before and after running the program is shown. You can see this is closer to your requirement. You can extend functionality of this program by finding Last Column in the Actively used range. Extend program functionality to cover upto last column.

skkakkar
  • 2,772
  • 2
  • 17
  • 30
  • This work is really close to my requierements. I just asked how can i start coding. You gave me a lot. Thanks for your interest, @skkakkar – Ylmz_eng Jun 11 '16 at 11:38
  • @Ylmz_eng I am glad that my post was helpful to you. Don't be disheartened by down votes. When I started here, I also faced the similar situation and have worked my way up to reach present level. – skkakkar Jun 11 '16 at 15:15