excel - In VBA, I want to make an If statement for when more than one cell in a range contains a value -


so have following vba loop set up, want add line says "if there 2 cells within range have value, this. if there 3 cells within range have value, that." have far is:

sub test1()      dim rng range     dim long     = 3     application.screenupdating = true      while <= 133         set rng = range("c" & i)          if rng.offset(, 2).resize(, 7) <> ""             rng.offset(, 1).formular1c1 = "blank"             = + 1         else: stop         end if     wend   end sub 

so have vba script print word "blank" appropriate cell if range empty. how can add more lines "if 1 cell in range contains value," or "if 2 cells in range contain value"

here how can check if there more 1 non-empty cell in given range:

if application.worksheetfunction.counta(rng.offset(, 2).resize(, 7)) > 1 

few additional tips code:

  • if know initial , final value of i should use for ... next loop instead of while ... wend. replace code:

    i = 3 '(...)  while <= 133     '(...)     = + 1  wend 

    with this:

    for = 3 133     '(...) next 
  • i think line of code cause type mismatch error:

    if rng.offset(, 2).resize(, 7) <> "" 

    because trying compare object of range type primitive value (empty string). avoid issue can use similar code above:

    if application.worksheetfunction.counta(rng.offset(, 2).resize(, 7)) = 0 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -