Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Row Values in an Array: VBA

Tags:

excel

vba

I'm building a Genetic Algorithm in VBA and my fitness function depends on how many 1's the array row has. The array is a combination of 1's and 0's. The Array can be any size (2D) and I need to add the number of 1s in the row and compare it to the rest of the values. I was considering finding the max value of 1s and comparing it the min values of 1 in the array and moving forward from there, but I'm not sure if this is the best way to do this.

If you guys could give me some tips on how to do this that would be great! Or if there are some array addition functions that I'm missing in VBA.

like image 493
Reece Edwards Avatar asked Apr 21 '26 22:04

Reece Edwards


1 Answers

Another approach would be to use Excel's SUM() function to do the work. Summing the array would give you the same answer as counting the ones, and you can use the Application.WorksheetFunction object to give you access to SUM():

x = Application.WorksheetFunction.Sum(aArray)
like image 146
Rob Gale Avatar answered Apr 25 '26 05:04

Rob Gale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!