title: How do I replace all the 0 values in a range with a formula depending on the category it's in?
body: <p>On an excel sheet, if there is a list of numbers and next to it a letter to determine what category it's in, how would I be able to change blank or 0 values with a formula depending on the category?</p>
  <p>In this case there's a list of price and weight for product a,b,and c. the average price for the products are already known and is in a table on the same excel sheet. To fill in the 0 data with an estimate of how much the product would've weighed, what would the code look like.</p>
  <p><a href="https://i.stack.imgur.com/CzD03.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CzD03.png" alt="An example" /></a></p>
  <pre><code>Sub test()
  
  Dim RNG As Range
  
   
  For Each RNG In Range(&quot;A2:A&quot;)
  
      If RNG.Value = &quot;0&quot; And RNG.Offset(0, 2) = &quot;a&quot; Then RNG.Offset(0, 0).Formula = &quot;=RC[1]/Cells(2,5)&quot;
  
      If RNG.Value = &quot;0&quot; And RNG.Offset(0, 2) = &quot;b&quot; Then RNG.Offset(0, 0).Formula = &quot;=RC[1]/Cells(3,5)&quot;
  
      If RNG.Value = &quot;0&quot; And RNG.Offset(0, 2) = &quot;c&quot; Then RNG.Offset(0, 0).Formula = &quot;=RC[1]/Cells(4,5)&quot;
      Next RNG
   
  End Sub
  </code></pre>
  <p>The real data is thousands of lines so manually doing it is not prefered. There are a few things like the RNG.Offest(0,0) that I'm not particularly happy about but it doesn't return a syntax error so i've stuck with it.</p>
  <p>Can anyone help me out?</p>