Convert String array to Integer array

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi all...but stuck on this one.

I have a text cell that contains rows that are to be addressed e.g.

"123,425,796"

I have dragged them into an array in vba using :

Code:
Public Function RowArray(rowrefs As String) As Boolean


Dim aryRowDirty() As String


aryRowDirty() = Split(rowrefs, ",")


If IsArrayEmpty(aryRowDirty()) Then
    RowArray = False
    Exit Function
End If

'Removes any blanks that sneak in
aryRow = CleanArray(aryRowDirty)


RowArray = True


End Function

But now would like to be able to build an array of integers - their true type. Or better still, built an array of integers in the first place...

Any ideas?
 
Maybe something like the below, but we'd need to see what you were doing to advise any better:
Code:
Dim x as long
dim arr() as int
redim arr(lbound(aryRowDirty) to Ubound(aryRowDirty)
For x = lbound(aryRowDirty) to ubound(aryRowDirty)
    arr(x) = cint(aryRowDirty(x))
Next x
 
Upvote 0
Just create another array typed as integer and with the same dimensions of your current array.

Then loop through both, assigning the values from the current array to the new array.
 
Upvote 0
Hi all...but stuck on this one.

I have a text cell that contains rows that are to be addressed e.g.

"123,425,796"

I have dragged them into an array in vba using :

Code:
Public Function RowArray(rowrefs As String) As Boolean


Dim aryRowDirty() As String


aryRowDirty() = Split(rowrefs, ",")


If IsArrayEmpty(aryRowDirty()) Then
    RowArray = False
    Exit Function
End If

'Removes any blanks that sneak in
aryRow = CleanArray(aryRowDirty)


RowArray = True


End Function

But now would like to be able to build an array of integers - their true type. Or better still, built an array of integers in the first place...

Any ideas?

They are not integers to begin with, rather, they are text representations of number concatenated together, so you can't make them integers when you pull them apart. But you have altenatives. First, whenever you need an element from the array to be an integer, you can force it to be one by using the CInt or CLng function (in practice, I never use Integer data types, only Long data types). For example...

Dim FiveTimesElement As Long
FiveTimesElement = 5 * CLng(aryRowDirty(1))

or you can let VB perform the coercion behind the scenes for you...

FiveTimesElement = 5 * aryRowDirty(1)

Another option is to declare the aryRowDirty array as Variant instead of String. While the assigned value will be a Variant with a subtype of String, VB will treat the number as a number if the statement it is used in requires it (this is very similar to the "behind the scenes coercion" mentioned above.
 
Upvote 0

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