PDA

View Full Version : [SOLVED:] Problem Evaluate Variables



bacter
12-14-2021, 06:58 PM
sub Test()

dim Variable1 as String
dim Variable2 as String

range("a1").value="Variable1"

Variable1="Hello Word"
Variable2=Evaluate(range("a1").value)

msgbox Variable2

end if

Evaluate dont work.

in cell a1 I have the text variable1


I want variable2 to be assigned the value of the variable written in a1


I have tried various ways and have not succeeded, any ideas?

p45cal
12-15-2021, 02:57 AM
Variable2=Range("A1").value
Is this what you're trying to do?:

Sub Test()
Dim Variable1 As String
Dim Variable2 As String

Variable1 = "Hello Word"
Range("a1").Value = Variable1
Variable2 = Range("a1").Value
MsgBox Variable2
End Sub

bacter
12-15-2021, 04:59 AM
but I need variable 2 to save "Hello world".

I can't find a way to evaluate the variable written in a1

Is this what you're trying to do?:

Sub Test()
Dim Variable1 As String
Dim Variable2 As String

Variable1 = "Hello Word"
Range("a1").Value = Variable1
Variable2 = Range("a1").Value
MsgBox Variable2
End Sub

p45cal
12-15-2021, 06:19 AM
but I need variable 2 to save "Hello world".

??!!
It does:
29228

Paul_Hossler
12-15-2021, 08:46 AM
but I need variable 2 to save "Hello world". I can't find a way to evaluate the variable written in a1






You don't need to 'Evaluate' A1 -- just use it







Variable2 = Range("a1").Value

大灰狼1976
12-24-2021, 05:18 AM
Maybe you need "CallByName"

Public Variable1 As String


Sub Test()


Dim Variable2 As String


Range("a1").Value = "Variable1"


Variable1 = "Hello Word"
Variable2 = CallByName(Me, Range("a1").Value, VbGet)


MsgBox Variable2


End Sub

bacter
12-28-2021, 05:50 AM
Thank you very much friend,
I was able to make it work, but only within the sheet1 module, or within a form,
from a module, I have not been able to do it.


the closest I could do it from a module was referencing the variable on sheet1,

Sub Test ()
Dim Variable2 As string
Range ("a1"). Value = "Variable1"
Sheets ("sheet1"). Variable1 = "Hello Word"
Variable2 = CallByName (Sheets ("sheet1"), Range ("a1"). Value, VbGet)
MsgBox Variable2
End Sub


Is there any way to do it inside the module without using code inside the sheet?


I really appreciate your help, thank you very much.

Bob Phillips
12-28-2021, 07:21 AM
Global Variable1 as String

Sub Test()

Variable1 = "Hello Word"
Range("A1").Value = Variable1

Msgbox Variable1

End Sub

大灰狼1976
12-29-2021, 04:19 AM
Hi bacter!
No way to do it inside the standard module, but there are two other ways.
Please refer to the attachments.

--Okami

bacter
12-29-2021, 12:25 PM
[Solved]
Thank you very much friends, I was able to do what I needed using the code from post #9 , it cannot be done directly in a module, but the solution with workbook or class works perfectly.



[Solved]

Paul_Hossler
12-29-2021, 08:30 PM
I'm glad you got it working (thanks 大灰狼1976 (http://www.vbaexpress.com/forum/member.php?70849-%26%2322823%3B%26%2328784%3B%26%2329436%3B1976) ), but

out of curiosity, what are you trying to accomplish by putting the name of a variable in the worksheet, and then setting the variable to a value?

It just seems to me to be a very round-about way

bacter
12-30-2021, 05:36 AM
Mr Paul_Hosseler

I am developing a Crypto-Trading Analysis software, I have a list of Modules, which I need to execute, at different times.


I did an example.29262


In column A, I have the names of the modules and in columns B-E I have the arguments of those modules


Dim C As New Class_VG
Public Stop1 As Boolean
Sub Test1(Var1 As Boolean, Var2 As String)
Var2 = CallByName(C, Var2, VbGet)
If Var1 = True Then
Debug.Print Var2
End If
End Sub


Sub Test2(Var1 As String, Var2 As Integer)
Var1 = CallByName(C, Var1, VbGet)
Debug.Print "The : " & Var1 & " is " & 3 * Var2
End Sub


Sub Test3_Change_Period(Var1 As String, Var2 As Integer)
Var1 = CallByName(C, Var1, VbGet)
C.Seconds = Var1 + Var2
Debug.Print C.Seconds
End Sub


Sub Initial()
Stop1 = False
C.Seconds = 1
C.Variable1 = "Hello word"
C.Variable2 = "Triple"
Call Cicle1
End Sub


Sub Cicle1()
Dim seconds2 As Integer
Dim Seconds1 As String
If Stop1 = False Then
Seconds1 = Time_in_format(C.Seconds)
SchedRecalc = Now + TimeValue(Seconds1)
Application.OnTime SchedRecalc, "Cicle2"
End If
End Sub
Sub Cicle2()
Dim Arg1 As Variant
Dim Arg2 As Variant
Dim Module_To_Run As String
Dim i As Integer
For i = 1 To 3
Module_To_Run = Cells(1 + i, 1).Value
Arg1 = Cells(1 + i, 2).Value
Arg2 = Cells(1 + i, 3).Value
Application.Run Module_To_Run, Arg1, Arg2
Next
Call Cicle1
End Sub
Function Time_in_format(Seconds1 As Integer)
If Seconds1 < 10 Then
Time_in_format = "00:00:0" & Seconds1
Else
Time_in_format = "00:00:" & Seconds1
End If
End Function
Sub STOP_Button()
Stop1 = True
End Sub

Class Module

Class_VG


Public Variable1 As String
Public Variable2 As String
Public Seconds As Integer

Paul_Hossler
12-30-2021, 01:22 PM
OK, I've always done .OnTime loops something like this




Option Explicit


Dim VarSeconds As Integer
Dim AllStop As Boolean
Dim VarTime As String


Sub START_Button()
If Not AllStop Then
VarSeconds = ActiveSheet.Cells(4, 3)
VarTime = Now + TimeValue("00:00:" & Format(VarSeconds, "00"))
Application.OnTime VarTime, "Cicle2"
End If
End Sub


Sub STOP_Button()
AllStop = True
End Sub


'----------------------------------------------------------------------------------------------------------


Sub Cicle2()
Debug.Print VarTime, "Cicle2"
With ActiveSheet
Application.Run .Cells(2, 1).Value, .Cells(2, 2), .Cells(2, 3), .Cells(2, 4), .Cells(2, 5)
Application.Run .Cells(3, 1).Value, .Cells(3, 2), .Cells(3, 3)
Application.Run .Cells(4, 1).Value, .Cells(4, 2), .Cells(4, 3)
End With


Call START_Button


End Sub


'--------------------------------------------------------------------------------------------------------


Sub Test1(Var1 As Boolean, Var2 As String, Var3 As String, Var4 As String)
If Var1 = True Then
Debug.Print VarTime, "Test1", Var2
End If
End Sub


Sub Test2(Var1 As String, Var2 As Integer)
Debug.Print VarTime, "Test2", "The : " & Var1 & " is " & Var2
End Sub


Sub Test3_Change_Period(Var1 As String, Var2 As Integer)
VarSeconds = Var2
Debug.Print VarTime, "Test3", VarSeconds
End Sub