Data:
This is only a sample from the data I am using.
Please find the output below. "Output via Code" is the one we are getting when we run the code. "Expected Output" is the one we are looking for.
The first code and its modified versions which I have pasted earlier seems to be giving the result but the only problem is, it is leaving one or two similar values without joining them.
Original Message:
I have framed a "If Statement". This statement compares two rows and displays a comment in one of the columns. While the statement works perfectly in excel, the code recorded in VBE for the same do not. Please help.
Statement:
=IF(D2=D3,(IF(G2+H2+G3+H3=0,CONCATENATE("Batch ","'",C2,"'"," has no earnings/hours"),(CONCATENATE(IF(G2=0,"",CONCATENATE("paying ","'",F2,"'"," earnings ",G2)),IF(H2=0,"",IF(H2<>0,IF(G2=0,CONCATENATE("paying ","'",F2,"'"," hours ",H2),CONCATENATE(" and hours ",H2)))),IF(G3=0,"",CONCATENATE(" , paying ","'",F3,"'"," earnings ",G3)),IF(H3=0,"",IF(H3<>0,IF(G3=0,CONCATENATE(" , paying ","'",F3,"'"," hours ",H3),CONCATENATE(" and hours ",H3)))))))),IF(D2<>D3,IF(G2+H2=0,CONCATENATE("Batch ","'",C2,"'"," has no earnings/hours"),CONCATENATE("Batch ","'",C2,"'",CONCATENATE(IF(G2=0,"",CONCATENATE(" paying ","'",F2,"'"," earnings ",G2)),IF(H2=0,"",IF(G2=0,CONCATENATE(" paying ","'",F2,"'"," hours ",H2),CONCATENATE(" and hours ",H2))))))))
- I tried recording a macro for this statement, but I get a error when I execute the recorded code in my macro.
- I would like to convert this statement into macro and run it as loop till end of last row.
- I would like to compare a particular cell in Row 1 and Row 2, then execute above statement. Then compare values of two different cells in Row 1 and Row 2, paste the outcome in anther cell. Once these tasks are performed, delete the second row and run the loop for similar cell values, join the first comments with second comment. If next row value is different then not to delete the row and run the loop and so on till end of all rows.
- Also, the second "if statement" isn't doing the job it is suppose to do.
Second If Statement:
=IF(D2<>D3,I2,IF(D2=D3,IF(AND(I2="Y",I3="Y"),"Y",lf(AND(I2="Y",I3="N"),"Y",IF(AND(I2="N",I3="Y"),"Y",IF(AND(I2="N",I3="N"),"N"))))))
VBE Code for If Statement 1:
ActiveCell.FormulaR1C1 = _
"=IF(RC[-6]=R[1]C[-6],(IF(RC[-3]+RC[-2]+R[1]C[-3]+R[1]C[-2]=0,CONCATENATE(""Batch "",""'"",RC[-7],""'"","" has no earnings/hours""),(CONCATENATE(IF(RC[-3]=0,"""",CONCATENATE(""paying "",""'"",RC[-4],""'"","" earnings "",RC[-3])),IF(RC[-2]=0,"""",IF(RC[-2]<>0,IF(RC[-3]=0,CONCATENATE("" paying "",""'"",RC[-4],""'"",""hours"", RC[-2]),CONCATENATE("" and hours "",RC[-2]))" & _
"1]C[-3]=0,"""",CONCATENATE("" , paying "",""'"",R[1]C[-4],""'"","" earnings "",R[1]C[-3])),(IF(R[1]C[-2]=0,"""",IF(R[1]C[-2]<>0,IF(R[1]C[-3]=0,CONCATENATE("" paying "","""",R[1]C[-4],""'"","" hours "",R[1]C[-2]),CONCATENATE("" and hours "",R[1]C[-2]))))))))),IF(RC[-6]<>R[1]C[-6],IF(RC[-3]+RC[-2]=0,CONCATENATE(""Batch "",""'"",RC[-7],""'"","" has no earnings/hours"")" & _
"CONCATENATE(""Batch "",""'"",RC[-7],""'"",CONCATENATE(IF(RC[-3]=0,"""",CONCATENATE("" paying "",""'"",RC[-4],""'"",""earnings "",RC[-3])),IF(RC[-2]=0,"""",IF(RC[-3]=0,CONCATENATE("" paying "",""'"",RC[-4],""'"","" hours "",RC[-2]),CONCATENATE("" and hours "",RC[-2]))))))))"
Code 1:
Sub CompareAndCompare()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet4")
Dim A As Range, B As Range, C As Range, D As Range, E As Range, F As Range
Dim compRange As Range: Set compRange = ws.Range("D2", ws.Cells(ws.Rows.Count, "D").End(xlUp))
Dim state1 As String
For Each A In compRange 'A = D2 on first iteration of the loop
Set B = A.Offset(1) 'B = D3
Set C = A.Offset(0, 3) 'C = G2
Set D = A.Offset(0, 4) 'D = H2
Set E = A.Offset(1, 3) 'E = G3
Set F = A.Offset(1, 4) 'F = H3
Set G = A.Offset(0, 5) 'G = I2
Set H = A.Offset(1, 5) 'H = I3
state1 = ""
If A.Value = B.Value Then
If G.Value = "N" And H.Value = "N" Then 'Statement 2
ws.Range("K" & A.Row).Value = "N"
Else
If G.Value = "" And H.Value = "Y" Then
ws.Range("K" & A.Row).Value = "Y"
Else
If G.Value = "Y" And H.Value = "" Then
ws.Range("K" & A.Row).Value = "Y"
Else
If G.Value = "N" And H.Value = "" Then
ws.Range("K" & A.Row).Value = "N"
Else
If G.Value = "" And H.Value = "N" Then
ws.Range("K" & A.Row).Value = "N"
Else
If G.Value = "" And H.Value = "" Then
ws.Range("K" & A.Row).Value = ""
Else: ws.Range("K" & A.Row).Value = "Y"
End If
End If
End If
End If
End If
End If
If C.Value + D.Value + E.Value + F.Value = 0 Then
state1 = "'" & ws.Range("F" & A.Row).Value & "', " & "'" & ws.Range("F" & A.Offset(1).Row).Value & "' has no earnings/hours"
Else
If C.Value <> 0 Then _
state1 = state1 & "paying '" & ws.Range("F" & A.Row).Value & "' earnings " & C.Value
If D.Value <> 0 Then
If C.Value = 0 Then
state1 = state1 & "paying '" & ws.Range("F" & A.Row).Value & "' earnings " & D.Value
Else
state1 = state1 & " and hours " & D.Value
End If
End If
If E.Value <> 0 Then _
state1 = state1 & " paying '" & ws.Range("F" & A.Offset(1).Row).Value & "' earnings " & E.Value
If F.Value <> 0 Then
If E.Value = 0 Then
state1 = state1 & " paying '" & ws.Range("F" & A.Offset(1).Row).Value & "' hours " & F.Value
Else
state1 = state1 & " and hours " & F.Value
End If
End If
End If
B.EntireRow.Delete
Else 'D2 <> D3
ws.Range("K" & A.Row).Value = G.Value 'Statement 2
If C.Value + D.Value = 0 Then
state1 = "'" & ws.Range("F" & A.Row).Value & "' has no earnings/hours"
Else
'state1 = "'" & ws.Range("F" & A.Row).Value & "'"
If C.Value <> 0 Then _
state1 = " paying '" & ws.Range("F" & A.Row) & "' earnings " & C.Value
If D.Value <> 0 Then
If C.Value = 0 Then
state1 = " paying '" & ws.Range("F" & A.Row) & "' hours " & D.Value
Else
state1 = " and hours " & D.Value
End If
End If
End If
End If
ws.Range("J" & A.Row).Value = state1
Next A
End Sub
Code 2:
Sub CompareAndCompare1()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet4")
Dim A As Range, B As Range
Dim compRange As Range: Set compRange = ws.Range("D2", ws.Cells(ws.Rows.Count, "D").End(xlUp))
Dim state1 As String
For Each A In compRange 'A = D2 on first iteration of the loop
Set B = A.Offset(1) 'B = D3
Set C = A.Offset(0, 7) 'C =K2
Set D = A.Offset(1, 7) 'D = K3
state1 = ""
If A.Value = B.Value Then
If C.Value = "N" And D.Value = "N" Then 'Statement 2
ws.Range("L" & A.Row).Value = "N"
Else
If C.Value = "" And D.Value = "Y" Then
ws.Range("L" & A.Row).Value = "Y"
Else
If C.Value = "Y" And D.Value = "" Then
ws.Range("L" & A.Row).Value = "Y"
Else
If C.Value = "N" And D.Value = "" Then
ws.Range("L" & A.Row).Value = "N"
Else
If C.Value = "" And D.Value = "N" Then
ws.Range("L" & A.Row).Value = "N"
Else
If C.Value = "" And D.Value = "" Then
ws.Range("L" & A.Row).Value = ""
Else: ws.Range("L" & A.Row).Value = "Y"
End If
End If
End If
End If
End If
End If
state1 = "Batch " & ws.Range("C" & A.Row).Value & ": " & ws.Range("J" & A.Row).Value & ", " & ws.Range("J" & A.Offset(1).Row).Value
B.EntireRow.Delete
Else 'D2<>D3
ws.Range("L" & A.Row).Value = C.Value
state1 = "Batch " & ws.Range("C" & A.Row).Value & ": " & ws.Range("J" & A.Row).Value
End If
ws.Range("M" & A.Row).Value = state1
Next A
End Sub
Code 3:
Sub CompareAndCompare2()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet4")
Dim A As Range, B As Range
Dim compRange As Range: Set compRange = ws.Range("A2", ws.Cells(ws.Rows.Count, "A").End(xlUp))
Dim state1 As String
For Each A In compRange 'A = A2 on first iteration of the loop
Set B = A.Offset(1) 'B = A3
Set C = A.Offset(0, 11) 'C =L2
Set D = A.Offset(1, 11) 'D = L3
state1 = ""
If A.Value = B.Value Then
If C.Value = "N" And D.Value = "N" Then 'Statement 2
ws.Range("N" & A.Row).Value = "N"
Else: ws.Range("N" & A.Row).Value = "Y"
End If
state1 = "For file# " & ws.Range("A" & A.Row).Value & ", " & ws.Range("M" & A.Row).Value & ", " & ws.Range("M" & A.Offset(1).Row).Value
B.EntireRow.Delete
Else 'A2<>A3
ws.Range("N" & A.Row).Value = C.Value
state1 = "For file# " & ws.Range("A" & A.Row).Value & ", " & ws.Range("M" & A.Row).Value
End If
ws.Range("O" & A.Row).Value = state1
Next A
End Sub



