Comparing 1 value with two other columns Loop

I3atnumb3rs

New Member
Joined
Nov 2, 2018
Messages
34
Hello,

I'm trying to create a loop that checks the names in column A with the names in column D and E. If The names in A are different from either D or E and the cells aren't empty I want to duplicate the line. I got the line duplicating, but it's not ignoring empty cells and duplicating anyway. Please help!

Sub DiffNames()


'Loop trough rows, if D != E create a new lines




Application.ScreenUpdating = False


Dim lngRow As Long
Dim Number

With ActiveSheet

lngRow = .Cells(65536, 1).End(xlUp).Row



Do

If IsEmpty(.Cells(lngRow, 4)) = False And _
IsEmpty(.Cells(lngRow, 5)) = False And _
.Cells(lngRow, 1) <> .Cells(lngRow, 4) = True Or _
.Cells(lngRow, 1) <> .Cells(lngRow, 5) = True Then


.Cells(lngRow, 1).EntireRow.Select
Selection.EntireRow.Copy
Selection.EntireRow.Insert Shift:=xlDown



End If




lngRow = lngRow - 1
Loop Until lngRow = 1

End With


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try :-
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Sep02
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
n = 1
[COLOR="Navy"]While[/COLOR] Cells(n, "A") <> ""
  [COLOR="Navy"]If[/COLOR] Not IsEmpty(Cells(n, "D")) And Not IsEmpty(Cells(n, "E")) [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not Cells(n, "A") = Cells(n, "D") Or Not Cells(n, "A") = Cells(n, "E") [COLOR="Navy"]Then[/COLOR]
        Cells(n, "A").EntireRow.Copy
        Cells(n, "A").EntireRow.Insert Shift:=xlDown
        n = n + 1
    [COLOR="Navy"]End[/COLOR] If
  [COLOR="Navy"]End[/COLOR] If
    n = n + 1
Wend
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
When my code is run on that data the 2nd ,3rd and 5th lines are duplicated , where is that incorrect ???
 
Upvote 0
When I run the code this is what I get, there are a few lines with only the name Victor in one column that don't duplicate: http://prntscr.com/p1sfu8

I changed my code around a bit and got it to do what I wanted :) Thanks for your time tho.

Sub DiffNames()


'Loop trough rows, if D != E create a new lines




Application.ScreenUpdating = False


Columns("D:E").Select
Selection.Replace What:="", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False


Dim lngRow As Long


With ActiveSheet

lngRow = .Cells(65536, 1).End(xlUp).Row



Do

If .Cells(lngRow, 1) <> .Cells(lngRow, 4) = True And _
.Cells(lngRow, 4) <> 1 = True Or _
.Cells(lngRow, 1) <> .Cells(lngRow, 5) = True And _
.Cells(lngRow, 5) <> 1 = True Then


.Cells(lngRow, 1).EntireRow.Select
Selection.EntireRow.Copy
Selection.EntireRow.Insert Shift:=xlDown


End If




lngRow = lngRow - 1
Loop Until lngRow = 1

End With

Columns("D:E").Select
Selection.Replace What:="1", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False



End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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