简体   繁体   中英

excel vba code to replace empty cell with text based on adjacent cells number range

Please help me, I'm frustrated with excel. Probably because I'm not that good with it. I've been tasked with the following. Take all blank cells in column c, reference the value in the adjacent cell in column b, and then print a statement in the cell.

I tried this function to no avail.

=IF(AND(C3 = "", 0 < B3 < 5000), "Order More", "Don't Order")

I got a circular error. So I thought I must need to use VBA.

Here are the snippets of code I have so far:

Worksheets("Sheet1").Activate

For Each rw In Sheet1.Rows
If rw.Columns("C") = "" and range.rw.Columns("B")

I'm not sure how to make it all work. I don't even know if that's correct. I've been trying to use the internet to help but it's just more confusing.

I don't think you want VBA.
Your formula should be

=IF(AND(C3 = "", 0 < B3, B3 < 5000), "Order More", "Don't Order")

If you're getting a circular error, it's because you're putting the formula in B3 or C3. It would logically be in A3 or D3?

Posting this as a response since

  • It is cumbersome to explain it on a comment
  • It is tested and it works for Excel 2010.

     Sub test() Dim lFirstBlank As Long, lLastRow As Long Dim rRange As Range lLastRow = Cells(Rows.Count, 3).End(xlUp).Row lFirstBlank = _ Range("C3:C" & lLastRow).SpecialCells(xlCellTypeBlanks).Cells(1, 1).Row Set rRange = Range("C" & lFirstBlank & ":C" & _ lLastRow).SpecialCells(xlCellTypeBlanks) rRange.Formula = _ "=IF(AND(B" & lFirstBlank & ">0,B" & lFirstBlank & "<5000)," & Chr(34) & _ "Order More" & Chr(34) & "," & Chr(34) & "Don't Order" & Chr(34) & ")" rRange.Value = rRange.Value End Sub 

The problem was that we need to find the first blank cell and enter the formula there with the reference relative to that cell (first blank), as I understand from your latest response. For instance, if the first blank cell is C5 , then the formula should refer to C5 and B5 , not C3 . The above code works as expected, assuming that the sheet we are looking at is the ActiveSheet (ie, it is activated).

EDIT

If we want to remove the dependency on column B , we can add the line

rRange.Value = rRange.Value

This will remove the formula dependencies and will keep only the values (like pasting only values).

The reasons why I did not write the formula as

=IF(AND(C3 = "", 0 < B3, B3 < 5000), "Order More", "Don't Order")

are that

  • It is going to give us a circular reference error, because we are using the value of C3 to determine the value of C3 .
  • Even if we avoid (or ignore) the circular reference, it does not seem to do what you want exactly: you want to to apply the logic 0 < B3 < 5000 to the blank cells as you mention in the question, and return either "Order More" or "Don't Order". So my understanding is that the non-blank cells should not be touched, whereas the above formula will overwrite whatever is written on the non-blank cells and write there "Don't Order".

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM