Multiplying a Cell base on Adjacent Column

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Folks,I have a very complex requirement. I need a macro to multiply based on some conditions. I have a two column one is filled with "Y" and "N" letters another one is filled with numbers. My requirement is to identify all the "Y" succeeded by "N" and multiply the value of adjacent cell to 'N" with values of cells adjacent to "Y". There could be lot of combinations of N decent by Y. See the example below.

Source Result

[TABLE="class: grid, width: 100, align: left"]
<tbody>[TR]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]5[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]1[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 100, align: center"]
<tbody>[TR]
[TD="align: center"]G
[/TD]
[TD="align: center"]H
[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]2
[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]4
[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]6
[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]5[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]5
[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]10
[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]15
[/TD]
[/TR]
[TR]
[TD="align: center"]N[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Y[/TD]
[TD="align: center"]1[/TD]
[/TR]
</tbody>[/TABLE]
 
Sorry I couldn't be of more assistance
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello Fluff, I identified the issue. Some of the cells in the H column contains TEXT values. Is there any fix to solve this?. Some times i am text values in H column
 
Upvote 0
Hi, here is another option that you can try..

Code:
Sub m()
Dim a, i As Long, m As Double
a = Range("G1:H" & Range("G" & Rows.Count).End(xlUp).Row).Value
For i = 1 To UBound(a)
  If IsNumeric(a(i, 2)) Then
    If UCase(a(i, 1)) = "N" Then m = a(i, 2)
    If UCase(a(i, 1)) = "Y" Then a(i, 2) = a(i, 2) * m
  End If
Next i
Range("G1:H1").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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