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

GH
N2
N3
N2
Y1
Y2
Y3
N2
N2
N2
Y1
N5
Y1
Y2
Y3
N1
Y1

<tbody>
</tbody>
G
H
N2
N3
N2
Y2
Y4
Y6
N2
N2
N2
Y2
N5
Y5
Y10
Y15
N1
Y1

<tbody>
</tbody>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about
Code:
Sub MultiplyCols()

   Dim Rng As Range
   Dim Cl As Range
   
   With Range("G1", Range("G" & Rows.Count).End(xlUp))
      .Replace "y", "#N/A", xlWhole, , False, , False, False
      For Each Rng In .SpecialCells(xlConstants, xlErrors).Areas
         For Each Cl In Rng.Offset(, 1)
            Cl.Value = Cl.Value * Rng.Offset(-1, 1).Resize(1, 1).Value
         Next Cl
      Next Rng
      .Replace "#N/A", "Y", xlWhole, , False, , False, False
   End With

End Sub
 
Upvote 0
Hi Fluff one small issue. If any of the cell in H column is empty the code is not running. It stopped after changing the G column cell values to #N/A. Any fix for this?
 
Upvote 0
I cannot replicate what you getting, what error message do you get?
 
Upvote 0
I didn't got any error. After the code runs the the cells having the Y letter not replacing back to Y, it stops in #N/A. The multiplication also not done. For better understanding delete any of the numerical value in column H adjacent to CELL contains Y. Then run the code you will get the error what i am trying to explain.
 
Upvote 0
When I do that all that happens, is that the col H cells next to a Y read as 0 (zero) & the #N/A values become Y again.
 
Upvote 0
It is not reading as Zero and the #N/A values not become Y again. But its not happening always, occasionally happening. When i fill the blank cells with some value the code is running properly.
 
Upvote 0
Unfortunately, I cannot replicate the problem you are getting. Which makes it difficult to figure out what is happening.
Do you have any worksheet or workbook events?
 
Upvote 0
Its OK Fluff i will fill the blank cells with zero before running the code. Thanks for your valuable time.
 
Upvote 0

Forum statistics

Threads
1,215,196
Messages
6,123,578
Members
449,108
Latest member
rache47

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