Copy if VBA

pietras152

New Member
Joined
Jul 31, 2013
Messages
13
Hi!

I am currently working on a quite big VBA aplication. One of the elements, however, made me crazy and I can't write a proper code.
Here is the problem:



I need to copy cells from 5 columns on the right when cells from first 5 columns are the same.
I know the problem might be easy but I have problem with inserting another clear rows below which depends on the amount of simillar rows above.
The colours are only to make this table easy to read and to depict the problem.

Here is the link to the data from image above.

Download 1.xlsx from Sendspace.com - send big files the easy way

Thank you for your attention. I really would appreciate any help!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
Sub a()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR To 2 Step -1
    Rows(r + 1).Insert
    Range("F" & r & ":J" & r).Cut Range("A" & r + 1 & ":E" & r + 1)
Next
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR - 1 To 4 Step -1
  If Range("A" & r) = Range("A" & r - 2) And Range("B" & r) = Range("B" & r - 2) _
  And Range("C" & r) = Range("C" & r - 2) And Range("D" & r) = Range("D" & r - 2) _
  And Range("E" & r) = Range("E" & r - 2) Then
    Rows(r).Delete
  End If
Next
End Sub
 
Upvote 0
Code:
Sub a()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR To 2 Step -1
    Rows(r + 1).Insert
    Range("F" & r & ":J" & r).Cut Range("A" & r + 1 & ":E" & r + 1)
Next
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR - 1 To 4 Step -1
  If Range("A" & r) = Range("A" & r - 2) And Range("B" & r) = Range("B" & r - 2) _
  And Range("C" & r) = Range("C" & r - 2) And Range("D" & r) = Range("D" & r - 2) _
  And Range("E" & r) = Range("E" & r - 2) Then
    Rows(r).Delete
  End If
Next
End Sub

I'm sorry but I have noticed considerable problem with this code.
Please look at this example:
Download example.xlsx from Sendspace.com - send big files the easy way


-- removed inline image ---


It occurs that when 5 cells from the columns on the right are the same for other rows they are deleted because they are treated as duplicates.

Thank you very much for help and I would be very grateful if you help me with this.
 
Upvote 0
Code:
Sub a()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR To 2 Step -1
    Rows(r + 1).Insert
    Range("F" & r & ":J" & r).Cut Range("A" & r + 1 & ":E" & r + 1)
Next
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = LR - 1 To 4 Step -1
  If Range("A" & r) = Range("A" & r - 2) And Range("B" & r) = Range("B" & r - 2) _
  And Range("C" & r) = Range("C" & r - 2) And Range("D" & r) = Range("D" & r - 2) _
  And Range("E" & r) = Range("E" & r - 2) [COLOR=#ff0000]And Range("A" & r) = "P" Then Rows(r).Delete[/COLOR]
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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