Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

How to read a string from a variant array ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Emmery - 12 Sep 2007 20:32 GMT
Need a hint how to get a string from a variant array.

The code I'm using (Excel 2003):
Dim MyArray(5) as variant
MyArray = array("help",1,2,3,4)
note: LBound (MyArr) = 0

Now I don't get anything when using:
MsgBox "Check: "+ MyArray(0)
or
MsgBox "Check: "+ CStr(MyArray(0))
or
MsgBox "Check: "+ CVar(MyArray(0))

How to get the string "help" from MyArr ?
JE McGimpsey - 12 Sep 2007 20:42 GMT
This works for me:

   Dim MyArray As Variant
   MyArray = Array("help", 1, 2, 3, 4)
   MsgBox "Check: " + MyArray(0)
   MsgBox "Check: " + CStr(MyArray(0))
   MsgBox "Check: " + CVar(MyArray(0))

> Need a hint how to get a string from a variant array.
>
[quoted text clipped - 11 lines]
>
> How to get the string "help" from MyArr ?
Bill Renaud - 12 Sep 2007 20:53 GMT
You don't need to specify the size of the array if you are going to use
the Array function to assign the values, since the compiler can figure
the size out at design time. You might need to use a REDIM later in the
program, if you add more values.

Public Sub Test()
 Dim MyArray As Variant

 MyArray = Array("help", 1, 2, 3, 4)

 MsgBox "Check: " & CStr(MyArray(0))
End Sub

I also changed the "+" to a "&" in your MsgBox statement, since it is
better practice when using strings, even though it is technically not
required.
(Someday you will have 2 variables that are supposed to be strings, but
are numbers, and addition will be performed instead.)
Signature

Regards,
Bill Renaud

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.