excel - doing logic operations in vba -


hi i'm trying logic operations or,not, , vba i'm having trouble code not incrementing values know probably(hopefully) easy fix error appreciated.

sub logicop ()  dim myrange range dim rowi range dim cell range  set myrange = range("a8:f20") 'go each row of column each rowi in myerange.rows      andfunc = 1 'and operaton     notfunc = 0 'not function     result1 = 0     result2 = 0     result3 = 0 each cell in rowi.cells  'go each cell of rows in rowi          if cell.value = notfunc          resuslt1 = result1 + 1          end if           if cell.value = andfunc          resuslt2 = result2 + 1          end if           if cell.value <> andfunc , cell.value <> notfun           result3 = result3 + 1           end if    next cell next     result1 = cells(3,3 )    result2 = cells(3, 4)    result3 = cells(3,5)  end sub 

as pointed out in comments there numerous spelling mistakes prevent code form working. can avoid using option explicit.

here cleaned version of code. tried keep close original exception of adding in select case

option explicit sub logicop()     dim wks worksheet     dim myrange range     dim rowi range     dim cell range     dim andfunc, notfunc, result1, result2, result3      set wks = sheets("sheet1")     set myrange = wks.range("a8:f20") 'go each row of column      andfunc = 1 'and operaton     notfunc = 0 'not function     result1 = 0     result2 = 0     result3 = 0      each rowi in myrange.rows         each cell in rowi.cells  'go each cell of rows in rowi             select case cell.value                 case notfunc                     result1 = result1 + 1                 case andfunc                     result2 = result2 + 1                 case else                     result3 = result3 + 1             end select         next cell     next rowi      'output results specific cells     wks.cells(3, 3).value = result1     wks.cells(3, 4).value = result2     wks.cells(3, 5).value = result3 end sub 

note empty cell counted 0 way it's written. i'm assuming have data in every cell within range wont problem.


example result:

enter image description here


edit

per comments, i've updated code count rows had 0's, 1's or mixed.

option explicit sub logicop()     dim wks worksheet     dim myrange range     dim rowi range     dim andfunc, notfunc, result1, result2, result3, rowsum      set wks = sheets("sheet1")     set myrange = wks.range("a8:f20") 'go each row of column      andfunc = 1 'and operaton     notfunc = 0 'not function     result1 = 0     result2 = 0     result3 = 0      each rowi in myrange.rows         rowsum = application.worksheetfunction.sum(rowi)          select case rowsum / rowi.cells.count             case notfunc                 result1 = result1 + 1             case andfunc                 result2 = result2 + 1             case else                 result3 = result3 + 1         end select     next rowi      'output results specific cells     wks.cells(3, 3).value = result1     wks.cells(3, 4).value = result2     wks.cells(3, 5).value = result3 end sub 

result

enter image description here


Comments

Popular posts from this blog

mongodb - How to keep track of users making Stripe Payments -

javascript - Fire on return from form "Submit" -

javascript - Thinglink image not visible until browser resize -