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
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,307
Office Version
  1. 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
35,503
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,924
Messages
5,525,661
Members
409,658
Latest member
Yardcell

This Week's Hot Topics

Top