PC Review


Reply
Thread Tools Rate Thread

How to convert the string to variable?

 
 
Christopher Panadol
Guest
Posts: n/a
 
      16th Jul 2008
I write VBA in Excel 2003. I have a program which have the serveral
variables, say, var1, var2, var3. Every time when I need to put a value to
these 3 variables I need to write the code as follows:

for i = 1 to 3
select case i
case 1
var1 = <value>
case 2
var2 = <value>
case 3
var3 = <value>
end select
next i

Is there a way to simplify the code that can convert the string to
variables? Sorry for my poor English. Thank you very much!


 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      16th Jul 2008
Maybe use an array instead of single variables, so you will get something
like this:

Dim i As Long
Dim arrLong(1 to 3) As Long

For i = 1 to 3
arrLong(i) = <value>
Next i


RBS


"Christopher Panadol" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I write VBA in Excel 2003. I have a program which have the serveral
>variables, say, var1, var2, var3. Every time when I need to put a value to
>these 3 variables I need to write the code as follows:
>
> for i = 1 to 3
> select case i
> case 1
> var1 = <value>
> case 2
> var2 = <value>
> case 3
> var3 = <value>
> end select
> next i
>
> Is there a way to simplify the code that can convert the string to
> variables? Sorry for my poor English. Thank you very much!
>


 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      16th Jul 2008
You can use an array

Sub Demo1()
Dim i as long, v(1 to 3) as Long
for i = 1 to 3
v(i) = int(rand()*10+1)
Next

for i = 1 to 3

msgbox "var(" & i & ") = " & var(i)
Next

end Sub


if you wanted to assign them based on ranges

i = 1
for each cell in Range("B1,C9,F7")
v(i) = cell.value
i = i + 1
Next

as an example.

--
Regards,
Tom Ogilvy

"Christopher Panadol" wrote:

> I write VBA in Excel 2003. I have a program which have the serveral
> variables, say, var1, var2, var3. Every time when I need to put a value to
> these 3 variables I need to write the code as follows:
>
> for i = 1 to 3
> select case i
> case 1
> var1 = <value>
> case 2
> var2 = <value>
> case 3
> var3 = <value>
> end select
> next i
>
> Is there a way to simplify the code that can convert the string to
> variables? Sorry for my poor English. Thank you very much!
>
>
>

 
Reply With Quote
 
J. Caplan
Guest
Posts: n/a
 
      16th Jul 2008
Instead of having 3 string variables, can you store the values in an Array

Dim vars(1 to 3) As String
for i = 1 to 3
vars(i) = <value>
next i

"Christopher Panadol" wrote:

> I write VBA in Excel 2003. I have a program which have the serveral
> variables, say, var1, var2, var3. Every time when I need to put a value to
> these 3 variables I need to write the code as follows:
>
> for i = 1 to 3
> select case i
> case 1
> var1 = <value>
> case 2
> var2 = <value>
> case 3
> var3 = <value>
> end select
> next i
>
> Is there a way to simplify the code that can convert the string to
> variables? Sorry for my poor English. Thank you very much!
>
>
>

 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      16th Jul 2008
sorry, got sloppy with the var's and v's


Sub Demo1()
Dim i as long, v(1 to 3) as Long
for i = 1 to 3
v(i) = int(rand()*10+1)
Next

for i = 1 to 3

msgbox "v(" & i & ") = " & v(i)
Next

end Sub


if you wanted to assign them based on ranges

i = 1
for each cell in Range("B1,C9,F7")
v(i) = cell.value
i = i + 1
Next

as an example.

You could use

Dim var(1 to 3) as long as well.

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote:

> You can use an array
>
> Sub Demo1()
> Dim i as long, v(1 to 3) as Long
> for i = 1 to 3
> v(i) = int(rand()*10+1)
> Next
>
> for i = 1 to 3
>
> msgbox "var(" & i & ") = " & var(i)
> Next
>
> end Sub
>
>
> if you wanted to assign them based on ranges
>
> i = 1
> for each cell in Range("B1,C9,F7")
> v(i) = cell.value
> i = i + 1
> Next
>
> as an example.
>
> --
> Regards,
> Tom Ogilvy
>
> "Christopher Panadol" wrote:
>
> > I write VBA in Excel 2003. I have a program which have the serveral
> > variables, say, var1, var2, var3. Every time when I need to put a value to
> > these 3 variables I need to write the code as follows:
> >
> > for i = 1 to 3
> > select case i
> > case 1
> > var1 = <value>
> > case 2
> > var2 = <value>
> > case 3
> > var3 = <value>
> > end select
> > next i
> >
> > Is there a way to simplify the code that can convert the string to
> > variables? Sorry for my poor English. Thank you very much!
> >
> >
> >

 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      16th Jul 2008
bad day

Sub Demo1()
Dim i As Long, v(1 To 3) As Long
For i = 1 To 3
v(i) = Int(Rnd() * 10 + 1)
Next

For i = 1 To 3

MsgBox "v(" & i & ") = " & v(i)
Next

End Sub

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote:

> sorry, got sloppy with the var's and v's
>
>
> Sub Demo1()
> Dim i as long, v(1 to 3) as Long
> for i = 1 to 3
> v(i) = int(rand()*10+1)
> Next
>
> for i = 1 to 3
>
> msgbox "v(" & i & ") = " & v(i)
> Next
>
> end Sub
>
>
> if you wanted to assign them based on ranges
>
> i = 1
> for each cell in Range("B1,C9,F7")
> v(i) = cell.value
> i = i + 1
> Next
>
> as an example.
>
> You could use
>
> Dim var(1 to 3) as long as well.
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Tom Ogilvy" wrote:
>
> > You can use an array
> >
> > Sub Demo1()
> > Dim i as long, v(1 to 3) as Long
> > for i = 1 to 3
> > v(i) = int(rand()*10+1)
> > Next
> >
> > for i = 1 to 3
> >
> > msgbox "var(" & i & ") = " & var(i)
> > Next
> >
> > end Sub
> >
> >
> > if you wanted to assign them based on ranges
> >
> > i = 1
> > for each cell in Range("B1,C9,F7")
> > v(i) = cell.value
> > i = i + 1
> > Next
> >
> > as an example.
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> > "Christopher Panadol" wrote:
> >
> > > I write VBA in Excel 2003. I have a program which have the serveral
> > > variables, say, var1, var2, var3. Every time when I need to put a value to
> > > these 3 variables I need to write the code as follows:
> > >
> > > for i = 1 to 3
> > > select case i
> > > case 1
> > > var1 = <value>
> > > case 2
> > > var2 = <value>
> > > case 3
> > > var3 = <value>
> > > end select
> > > next i
> > >
> > > Is there a way to simplify the code that can convert the string to
> > > variables? Sorry for my poor English. Thank you very much!
> > >
> > >
> > >

 
Reply With Quote
 
Christopher Panadol
Guest
Posts: n/a
 
      16th Jul 2008
Dears,

I need to say sorry that I was the FoxPro programmer before. I feel very
comfortable that using the way of storing information by locating the
variable address using "&" function. I have certainly considered using
array method. It is, however, necessary for me to use this
string-to-variable method.

Anyway, thank you for your answer.

Chris

"Christopher Panadol" <(E-Mail Removed)> ¼¶¼g©ó¶l¥ó·s»D:%(E-Mail Removed)...
>I write VBA in Excel 2003. I have a program which have the serveral
>variables, say, var1, var2, var3. Every time when I need to put a value to
>these 3 variables I need to write the code as follows:
>
> for i = 1 to 3
> select case i
> case 1
> var1 = <value>
> case 2
> var2 = <value>
> case 3
> var3 = <value>
> end select
> next i
>
> Is there a way to simplify the code that can convert the string to
> variables? Sorry for my poor English. Thank you very much!
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate without string or convert string to variable =?Utf-8?B?U1NCU3lzdGVtc0NsZXJr?= Microsoft Access Form Coding 5 6th Apr 2006 11:46 PM
Re: Convert String to variable contained in string Bruce Wood Microsoft C# .NET 0 14th Apr 2005 08:13 PM
Re: Convert String to variable contained in string Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 0 14th Apr 2005 07:31 PM
Connection String object Convert to String Variable Type =?Utf-8?B?TWlrZSBNb29yZQ==?= Microsoft ASP .NET 2 26th Oct 2004 03:43 PM
How do I convert an integer variable to a string variable? =?Utf-8?B?ZHVtYmFzcw==?= Microsoft Excel Programming 2 21st May 2004 07:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:17 AM.