Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2012, 09:03 AM
tinfanide tinfanide is offline Convert String Array to Integer Array from a User Input? Windows 7 64bit Convert String Array to Integer Array from a User Input? Office 2010 32bit
Expert
Convert String Array to Integer Array from a User Input?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default Convert String Array to Integer Array from a User Input?

Code:
Sub q()

Dim vInput As Variant
vInput = InputBox("", "", "9,1,3,5,6,2,4,10,7,8")

Dim arr As Variant, a As Variant
arr = Split(vInput, ",")

Debug.Print TypeName(arr) '' String()

For a = LBound(arr) To UBound(arr)
    arr(a) = CInt(arr(a)) '' to convert string to integer
    Debug.Print TypeName(arr(a)) '' String
Next a

End Sub
This is a general VBA question.
I ask it here. I code the above in Word though.



I want to use CInt() to convert each item in an array from string to integer but it fails.
How can I convert a string array to an integer array?

Thank you.
Reply With Quote
  #2  
Old 12-26-2012, 02:18 AM
macropod's Avatar
macropod macropod is offline Convert String Array to Integer Array from a User Input? Windows 7 64bit Convert String Array to Integer Array from a User Input? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub q()
Dim vInput As Variant
vInput = InputBox("", "", "9.1,1,3,5,6,2,4,10,7,8")
Dim arr(), a As Long
For a = 0 To UBound(Split(vInput, ","))
  ReDim Preserve arr(a)
    arr(a) = CInt(Split(vInput, ",")(a)) '' to convert string to integer
    MsgBox arr(a) & vbTab & TypeName(arr(a))
Next a
End Sub
Hint: Storing integers as longs makes for more efficient processing, so consider:
arr(a) = CLng(Split(vInput, ",")(a))
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-26-2012, 06:02 AM
tinfanide tinfanide is offline Convert String Array to Integer Array from a User Input? Windows 7 64bit Convert String Array to Integer Array from a User Input? Office 2010 32bit
Expert
Convert String Array to Integer Array from a User Input?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Try:
Hint: Storing integers as longs makes for more efficient processing, so consider:
arr(a) = CLng(Split(vInput, ",")(a))
Yes, it works. And by the way, I worked it out much the way like you do here, creating a new Long array and loop through it with the old String array.
Thanks for your hint, not that I didn't really know what Long is.
Reply With Quote
  #4  
Old 12-26-2012, 03:41 PM
macropod's Avatar
macropod macropod is offline Convert String Array to Integer Array from a User Input? Windows 7 64bit Convert String Array to Integer Array from a User Input? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A 'Long' is an variable that can store larger integer values than an 'Integer' variable. In more recent versions of Office, 'Integer' variables are actually stored in memory as Long variables. What this means is that a type conversion is required to convert 'Integer' variables to/from Long variables each time they're used. That type conversion has a small processing overhead.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-26-2012, 08:56 PM
tinfanide tinfanide is offline Convert String Array to Integer Array from a User Input? Windows 7 64bit Convert String Array to Integer Array from a User Input? Office 2010 32bit
Expert
Convert String Array to Integer Array from a User Input?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
A 'Long' is an variable that can store larger integer values than an 'Integer' variable. In more recent versions of Office, 'Integer' variables are actually stored in memory as Long variables. What this means is that a type conversion is required to convert 'Integer' variables to/from Long variables each time they're used. That type conversion has a small processing overhead.
Oh, I see. Thanks.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array argument in macro command Wries Excel Programming 2 11-20-2012 01:08 AM
How to reserve an array in a public function? tinfanide Word VBA 2 02-27-2012 06:51 AM
Convert String Array to Integer Array from a User Input? Complex array formula andrei Excel 9 02-03-2012 03:40 AM
How to capture start and ending Ref. Nos. in an array KIM SOLIS Excel 5 09-07-2011 07:43 AM
Look up an array based on user input johnsmb Excel 2 01-07-2011 01:12 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft