vba Odd's and Even"s Array Count If

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I have a dynamic array B2:G2600 (6 columns), on Col "M" would like to see how many odd numbers there are in the rows, and on Col "N" the even numbers.
So I workout first with the macro recorder, ("this is a short version as example, only"),
VBA Code:
 Sub CORR_ECT()Range("H2").Select
   Selection.FormulaArray = "=COUNT(IF(MOD(A2:F2,2)=0,A2:H2))" ''''EVENS
        Range("I2").Select
Selection.FormulaArray = "= COUNT(IF(MOD(A2:F2,2),A2:I2))"  '''''ODDS
End Sub

and the example data
BCDEFGODDEVEN
212345633
3789101112
4131415161718
5192021222324
6252627282930
7313233343536
8373839404142
9434445464748

So I am trying to loop this array, but of course do not work, this is my code,
VBA Code:
  Sub OD_EV()      Dim X As Integer, J As Integer
      finalRow = Cells(Row.Count, 2).End(xlUp).Row
               For X = 2 To finalRow
               For J = 1 To 6
                        Range("H" & X) = FormulaArray = "=COUNT(IF(MOD(X:J,2)=0,X:J2))" ''''EVENS
                        Range("I" & X) = FormulaArray = "=COUNT(IF(MOD(X:J,2),X:J2))" 'ODDS
               Next J
               Next X
End Sub
Thank you for reading this.
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Are you required to use VBA to solve this? If not, you can put these formulas in the indicated cells and copy them down to the end of your data...

M2: =SUMPRODUCT(--(MOD(B2:G2,2)=1))

N2: =SUMPRODUCT(--(MOD(B2:G2,2)=0))
 
Upvote 0
Thank you, Rick.
Yes I need to see the code.
Its the not the Destination, It's the journey
I really learn more from the code, the logic behind, the transformation from English to VBA is what count most
Thanks.
 
Upvote 0
How about
Code:
Sub montecarlo2012()
   Dim Ary As Variant
   Dim r As Long, c As Long, Ev As Long, Od As Long
   
   Ary = Range("B2", Range("B" & Rows.Count).End(xlUp).Offset(, 7)).Value2
   
   For r = 1 To UBound(Ary)
      For c = 1 To UBound(Ary, 2) - 2
         If Application.IsEven(Ary(r, c)) Then Ev = Ev + 1 Else Od = Od + 1
      Next c
      Ary(r, 7) = Od: Ary(r, 8) = Ev
      Od = 0: Ev = 0
   Next r
   Range("B2").Resize(UBound(Ary), UBound(Ary, 2)).Value = Ary
End Sub
 
Upvote 0
Thank you Fluff (MrExcel MVP).
Is really Nice from you to give me this code. Important lesson I got here, first I try to change the result locations, so when I see Ary(r, 7) my Visual Logic thought about, Col H if vba count from "0" this is 7, and 8, then when I change to 13 or Col M that is the place I really need this, nothing happen, then I saw the line Offset(, 7), I was thinking here it is, nothing either, So here I am, my question here is what to change in order to move the results to Column ("M2") and ("N2"). PLEASE.
I can see Ary is like finalRow = etc. the rest I have to study more,
I am really impress you named the sub as montecarlo, thank you,
Great Lesson.
 
Upvote 0
How about
Code:
Sub montecarlo2012()
   Dim Ary As Variant
   Dim r As Long, c As Long, Ev As Long, Od As Long
   
   Ary = Range("B2", Range("B" & Rows.Count).End(xlUp).Offset(, [COLOR=#ff0000]12[/COLOR])).Value2
   
   For r = 1 To UBound(Ary)
      For c = 1 To UBound(Ary, 2) - [COLOR=#ff0000]7[/COLOR]
         If Application.IsEven(Ary(r, c)) Then Ev = Ev + 1 Else Od = Od + 1
      Next c
      Ary(r, [COLOR=#ff0000]12[/COLOR]) = Od: Ary(r, [COLOR=#ff0000]13[/COLOR]) = Ev
      Od = 0: Ev = 0
   Next r
   Range("B2").Resize(UBound(Ary), UBound(Ary, 2)).Value = Ary
End Sub
 
Upvote 0
THANK YOU SIR. You are really Nice, Thank you for your time. work perfect, I See you highlight the changes, wow. I hope you have a great day.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
How about
Code:
Sub montecarlo2012()
   Dim Ary As Variant
   Dim r As Long, c As Long, Ev As Long, Od As Long
   
   Ary = Range("B2", Range("B" & Rows.Count).End(xlUp).Offset(, [COLOR=#ff0000]12[/COLOR])).Value2
   
   For r = 1 To UBound(Ary)
      For c = 1 To UBound(Ary, 2) - 7
         If [B][COLOR="#FF0000"]Application.IsEven(Ary(r, c))[/COLOR][/B] Then Ev = Ev + 1 Else Od = Od + 1
      Next c
      Ary(r, 12) = Od: Ary(r, 13) = Ev
      Od = 0: Ev = 0
   Next r
   Range("B2").Resize(UBound(Ary), UBound(Ary, 2)).Value = Ary
End Sub

Just noting as a point of interest that as an alternative, what I highlighted in red can be replaced with this...

Ary(r, c) Like "*[02468]"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top