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
(i.e., 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!