Merging rows quickly

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

im setting up a tool and one of the steps is to merge some data together.

It's a large document and my initial idea was to cycle back from last row, and check 2 cells, if they match between the 2 rows, a 3rd cell merges both row values and deletes the old one.
[TABLE="width: 272"]
<tbody>[TR]
[TD]row nr /[/TD]
[TD]1st value/[/TD]
[TD]2nd value/[/TD]
[TD]merge value[/TD]
[/TR]
[TR]
[TD="align: right"]3000[/TD]
[TD]/random[/TD]
[TD]/match/[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]3001[/TD]
[TD]/random[/TD]
[TD]/match/[/TD]
[TD="align: right"]2
[/TD]
[/TR]
</tbody>[/TABLE]
result:3000 /random/ match/ 3

This is too slow and I'm not sure how to approach it, the data size will only increase through out time.

Going row by row seems to be inefficient, but I can't think of a better way
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
What code do you have at the moment?
 
Upvote 0
What code do you have at the moment?

Hello Fluff, sorry for the late reply, code:

Code:
 For counter1 = lastRow To 1 Step -1
        cell1Value = Range("E" + CStr(counter1))
        cell3Value = Range("A" + CStr(counter1))
        lastCycleCounter = Range("A" + CStr(counter1)).Address
        If Right(lastCycleCounter, 2) <> "$1" Then
            Cell2Value = Range("E" + CStr(counter1 - 1))
            cell4Value = Range("A" + CStr(counter1 - 1))
            If cell1Value = Cell2Value And cell3Value = cell4Value And Range("C" + CStr(counter1 - 1)) = Empty Then
                var1 = Range("G" + CStr(counter1)).Value
                var2 = Range("G" + CStr(counter1 - 1)).Value
                sumOfUnits = var1 + var2
                Range("G" + CStr(counter1 - 1)).Value = sumOfUnits
                Rows(counter1 & ":" & counter1).Delete Shift:=xlDown
            End If
        End If
    Next counter1
 
Upvote 0
Ended up with this:

Code:
    ReDim Preserve snapshotArrayFull(2 To lastRow, 1 To lastColumn)
    For i = 2 To lastRow
        If Cells(i, 5).Value = Cells(i - 1, 5).Value And Cells(i, 1).Value = Cells(i - 1, 1).Value Then
        snapshotArrayFull(positionT - 1, 7) = snapshotArrayFull(positionT - 1, 7) + Cells(i, 7)
        i = i + 1
        Else:
        For j = 1 To lastColumn
            snapshotArrayFull(positionT, j) = Cells(i, j).Value
        Next j
        positionT = positionT + 1
        End If
    Next i

Seems to be around 10 times faster than the prior approach
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

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