Macro to concatenate a varying number of cells

salasv

New Member
Joined
Feb 11, 2013
Messages
13
Hi,

I have a monthly report which shows our internal tickets and corresponding vendor tickets. Some of our internal tickets may not have a vendor ticket or may have only one vendor ticket, while others will have two or more vendor tickets. I've included a sample of the data below. I want column E to show the vendor tickets and to consolidate rows with 2 or more vendor tickets to one row.

I was thinking there might be a way to do with this a function if I had the count of vendor tickets per internal ticket, so I created the following function in column B. This probably won't be necessary for the macro, but here it is just in case. =IF(AND(C2=C1,ISNUMBER(B1)=TRUE),B1+1,IF(AND(C2=C1,C2=C3,ISNUMBER(B1)=TRUE),B1+1,IF(C2=C3,1,"")))

The amount of data will vary every month, so the macro will have to be able to handle varying numbers of rows. I'm also including this step as part of a larger macro to format the data, so any additional information on incorporating a macro for this purpose would be helpful.


ABCDE
DATE_SUBMITTEDInternalVendorVendor Consolidated
6/1/2013 6:157547170061
6/2/2013 10:5375472
6/8/2013 23:3275552
6/2/2013 15:3275661
6/3/2013 11:597566270209
6/5/2013 5:2375752
6/2/2013 19:5675861
6/5/2013 10:5475863
6/5/2013 22:087586470230
6/6/2013 6:337586570269
6/7/2013 14:0975866
6/3/2013 8:5575964
6/2/2013 13:0176046
6/3/2013 19:0576047
6/3/2013 12:307614670212
6/4/2013 6:167614770209
6/4/2013 6:1617614870215
6/4/2013 6:1627614870259
6/4/2013 6:1637614870272
6/4/2013 6:3976149
6/4/2013 11:247615070232
6/4/2013 12:3676151
6/5/2013 6:167615370234
6/5/2013 6:167615470236
6/5/2013 6:547615570246
6/5/2013 7:4276156
6/5/2013 12:4517615770249
6/5/2013 12:4527615770254
6/5/2013 15:037615970239
6/6/2013 6:157616070247
6/6/2013 9:277616270268

<tbody>
</tbody>


Thank you, in advance, for your time and assistance!!
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I found macro code on another website (macros - Combine multiple rows with same value into one plus more... in Excel - Stack Overflow) provided by Chris Neilsen and adapted it for my data and appearance. It works for the general idea, but deletes the entire sheet and replaces it with just data in columns C & D. I have 21 columns of data with 983 rows so far this month, so I can't have it delete the sheet. I need it to just delete the duplicate rows or to write the data to first cell of the duplicate internal ticket, so I can delete duplicates or some other genius idea you'll have. Any suggestions on further amending this to make it work for my needs?

Code:
Sub MergeRows()
    Dim rng As Range
    Dim vSrc As Variant
    Dim vDst() As Variant
    Dim i As Long, j As Long

    ' Assumes data starts at cell C2 and extends down with no empty cells
    Set rng = Range([C2], [C2].End(xlDown))

    ' Count unique values in column C
    j = Application.Evaluate("SUM(IF(FREQUENCY(" _
        & rng.Address & "," & rng.Address & ")>0,1))")
    ReDim vDst(1 To j, 1 To 2)
    j = 1

    ' Get original data into an array
    vSrc = rng.Resize(, 2)

    ' Create new array, one row for each unique value in column A
    vDst(1, 1) = vSrc(1, 1)
    vDst(1, 2) = "'" & vSrc(1, 2)
    For i = 2 To UBound(vSrc, 1)
        If vSrc(i - 1, 1) = vSrc(i, 1) Then
            vDst(j, 2) = vDst(j, 2) & Chr(10) & vSrc(i, 2)
        Else
            j = j + 1
            vDst(j, 1) = vSrc(i, 1)
            vDst(j, 2) = "'" & vSrc(i, 2)
        End If

    Next

    ' Remove old data
    rng.EntireRow.Delete

    ' Put new data in sheet
    Set rng = [C2].Resize(j, 2)
    rng = vDst

End Sub
 
Upvote 0
Sorry for my delay. I tried replying to this and I guess I had problems because my reply didn't post.

I did try removing the rng.EntireRow.Delete but this caused the data to be out-of-synch with the other information. The data is written back to the column and not lined up to original rows it was on because the duplicate rows are left behind. It also overwrites the original data, so I'm not able to figure out which row it corresponded to.
 
Upvote 0

Forum statistics

Threads
1,217,409
Messages
6,136,457
Members
450,013
Latest member
k4kamal

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top