Merge Rows in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Is it possible in VBA to merge rows that contain a mutual text but different
value ?
Ex:
Column: A B C
ROW 1 "paid" $300.00
ROW 2 "paid" $200.00

Can I merge row 1 and row 2 in row 3 and to do a sum in Column C?
Row 3 "paid" $500.00
Jeff,
 
Maybe you could use something like this ...


Code
-------------------
Option Explicit
Sub transferMe()
Dim ans As Double, lastRow As Long, i As Long
lastRow = Range("A65536").End(xlUp).Row
ans = 0
For i = lastRow To 1 Step -1
If Range("A" & i).Value = "paid" Then
ans = ans + Range("B" & i).Value
If WorksheetFunction.CountIf(Range("A1:A" & lastRow), "paid") <> 1 Then
Range("A" & i).EntireRow.Delete
Else
Range("B" & i).Value = ans
End If
End If
Next i
End Su
 
This will do both paid and unpaid.. ..


Code
-------------------
Option Explicit
Sub transferMe()
Dim ans As Double, lastRow As Long, i As Long, negans As Double
lastRow = Range("A65536").End(xlUp).Row
ans = 0
For i = lastRow To 1 Step -1
If Range("A" & i).Value = "paid" Then
ans = ans + Range("B" & i).Value
If WorksheetFunction.CountIf(Range("A1:A" & lastRow), "paid") <> 1 Then
Range("A" & i).EntireRow.Delete
Else
Range("B" & i).Value = ans
End If
End If
If Range("A" & i).Value = "unpaid" Then
negans = negans + Range("B" & i).Value
If WorksheetFunction.CountIf(Range("A1:A" & lastRow), "unpaid") <> 1 Then
Range("A" & i).EntireRow.Delete
Else
Range("B" & i).Value = negans
End If
End If
Next i
End Sub

-------------------


Please note, these sub routines will delete the entire row of ever
match until there is only 1 value of 'paid' and 'unpaid' un column A.
It also assumes your data starts in row 1 and goes down
 
Thank you for your help.

One more thing. What about if I wanted to have your VBA formula but more
generi: Let's say I don't know what the common text is, but I still want to
merge all rows with common text in colum "A"
is that possible ?
Thanks,
Jeff
 
I'm not sure you need VBA for this at all. Won't SUMIF do what you want? Or,
if you have two criteria, a SUMPRODUCT formula or array formula? If any of
those worksheet-function solutions will do, they will be much faster than VBA.


Thank you for your help.

One more thing. What about if I wanted to have your VBA formula but more
generi: Let's say I don't know what the common text is, but I still want to
merge all rows with common text in colum "A"
is that possible ?
Thanks,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top