Beginner vba question

West Man

Well-known Member
Joined
Mar 27, 2006
Messages
1,175
Beginner at VBA. I have a string variable named Result and an array - Dim myarray(1 To 56) As Integer.

1. Can I load the string (56 characters) into the array (with each character of the string resulting as an element) as simply as myarray = result

2. What is the best way to sum the elements of the array - they will be only ones and zeros
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,866
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Beginner at VBA. I have a string variable named Result and an array - Dim myarray(1 To 56) As Integer.

1. Can I load the string (56 characters) into the array (with each character of the string resulting as an element) as simply as myarray = result

2. What is the best way to sum the elements of the array - they will be only ones and zeros

If you have a string of ones and zeroes, and you want a count of how many ones and zeroes there are, then no array is needed...

Code:
S = "11010101101010110100101000101011010101101010110100101010"
NumberOfOnes = Len(Replace(S,"0",""))
NumberOfZeroes = Len(Replace(S,"1",""))
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,772
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
No. You can load a string into a byte array in one pass but it will contain the character codes, not the literal text. Or, if the string is delimited in some way you can use split but the resultant array will be a String array not Integer. For so few elements I'd just loop or try and alter the method of creating the string in the first place to create an array instead.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,487
Messages
5,831,981
Members
430,100
Latest member
namhnz

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
Top