excel vba code to replace empty cell with text based on adjacent cells number range -
please me, i'm frustrated excel. because i'm not it. i've been tasked following. take blank cells in column c, reference value in adjacent cell in column b, , print statement in cell.
i tried function no avail.
=if(and(c3 = "", 0 < b3 < 5000), "order more", "don't order")
i got circular error. thought must need use vba.
here snippets of code have far:
worksheets("sheet1").activate each rw in sheet1.rows if rw.columns("c") = "" , range.rw.columns("b")
i'm not sure how make work. don't know if that's correct. i've been trying use internet it's more confusing.
posting response since
- it cumbersome explain on comment
it tested , works excel 2010.
sub test() dim lfirstblank long, llastrow long dim rrange 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 need find first blank cell , enter formula there reference relative cell (first blank), understand latest response. instance, if first blank cell c5
, formula should refer c5
, b5
, not c3
. above code works expected, assuming sheet looking @ activesheet
(i.e., activated).
edit
if want remove dependency on column b
, can add line
rrange.value = rrange.value
this remove formula dependencies , keep values (like pasting values).
the reasons why did not write formula
=if(and(c3 = "", 0 < b3, b3 < 5000), "order more", "don't order")
are that
- it going give circular reference error, because using value of
c3
determine value ofc3
. - even if avoid (or ignore) circular reference, not seem want exactly: want to apply logic
0 < b3 < 5000
blank cells mention in question, , return either "order more" or "don't order". understanding non-blank cells should not touched, whereas above formula overwrite whatever written on non-blank cells , write there "don't order".
i hope helps!
Comments
Post a Comment