Compare Two column and put the value in 3rd column

ARUNCHENNAI

Board Regular
Joined
Apr 24, 2017
Messages
66
Hi,
I want the desired result in Z column though VBA
[TABLE="width: 254"]
<tbody>[TR]
[TD]X[/TD]
[TD]Y[/TD]
[TD]Z[/TD]
[/TR]
[TR]
[TD]Plan[/TD]
[TD]Actual[/TD]
[TD]Desired Result[/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]10[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]20[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"]20[/TD]
[/TR]
[TR]
[TD="align: right"]30[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD="align: right"]40[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]50[/TD]
[TD][/TD]
[TD="align: right"]50[/TD]
[/TR]
[TR]
[TD="align: right"]60[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]70[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]80[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]90[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD="align: right"]0[/TD]
[/TR]
</tbody>[/TABLE]

My code is not working. Kindly help.
Code:
Sub test()Dim X As Range, Y As Range, Z As Range


For Each X In Range("A5:A20")
    For Each Y In Range("B5:B20")
        For Each Z In Range("C5:C20")
            If X = 0 Then
            Z = 0
            'End If
                    Else
                If X <> 0 Then
                Z = X.Value
            End If
            End If
        Next Z
    Next Y
Next X


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Perhaps you could explain what is supposed to happen. From looking at your data, I do not see any kind of consistent relationship between the column values...
 
Upvote 0
igold, thank you for your time.
I'm sorry for not making the request clearly.

What I have is X column the plan data and Y column as actual data.
I need to display in Z column against X and Y row, if plan is 0 then Z column need to be filled with 0, else to fetch actual data in Z column in order.
 
Upvote 0
if X is not zero and Y is empty, what does Z equal...
 
Upvote 0
Hi,
I found a work around with below code.Forking fine.
Any suggestion to do do it better.
Code:
Sub test()
Dim x As Range
Dim i As String

For Each x In Range(""A1:A15")
i = x.Address
  If x = 0 Then
   Range(i).Offset(, 1).Insert Shift:=xlDown
  End If
  Next x
  MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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