As @BigBen said in the comments, it is down to your use of If...Else...If
twice. The first correction is:
If Result = True And Worksheets("Sheet3").Range("C4", Range("C4").End(xlDown)) <> "" Then
Worksheets("Sheet1").Range("C4", Range("C4").End(xlDown)).Value = Worksheets("Sheet1").Range("C4", Range("C4").End(xlDown)). _
Value + Worksheets("Sheet3").Range("C4", Range("C4").End(xlDown)).Value
ElseIf Result = True And Worksheets("Sheet3").Range("D4", Range("D4").End(xlDown)) <> "" Then
Worksheets("Sheet1").Range("D4", Range("D4").End(xlDown)).Value = Worksheets("Sheet1").Range("D4", Range("D4").End(xlDown)). _
Value + Worksheets("Sheet3").Range("D4", Range("D4").End(xlDown)).Value
End If
Notice how your use of Else
and If
on separate lines has been replaced by ElseIf
. And the second change is:
If Result = True And Worksheets("Sheet3").Range("C4", Range("C4").End(xlDown)) <> "" Then
Worksheets("Sheet2").Range("C4", Range("C4").End(xlDown)).Value = Worksheets("Sheet2").Range("C4", Range("C4").End(xlDown)). _
Value + Worksheets("Sheet3").Range("C4", Range("C4").End(xlDown)).Value
ElseIf Result = True And Worksheets("Sheet3").Range("D4", Range("D4").End(xlDown)) <> "" Then
Worksheets("Sheet2").Range("D4", Range("D4").End(xlDown)).Value = Worksheets("Sheet2").Range("D4", Range("D4").End(xlDown)). _
Value + Worksheets("Sheet3").Range("D4", Range("D4").End(xlDown)).Value
End If
Also, you are declaring val1
,val2
and result
twice in this sub – you only need to declare them once.
Regards,
6
solved Automate Quantity of any Items Using Excel VBA [closed]