Consulting

Results 1 to 13 of 13

Thread: Problem Evaluate Variables

  1. #1
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    5
    Location

    Lightbulb Problem Evaluate Variables

    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?
    Last edited by Bob Phillips; 12-28-2021 at 07:18 AM. Reason: Add code tags

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    5
    Location
    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

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by bacter View Post
    but I need variable 2 to save "Hello world".
    ??!!
    It does:
    2021-12-15_131845.png
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by bacter View Post
    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
    Last edited by Paul_Hossler; 12-15-2021 at 12:16 PM. Reason: Trying to fix formatting
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    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

  7. #7
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    5
    Location
    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.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Global Variable1 as String
    
    Sub Test()
    
        Variable1 = "Hello Word"
        Range("A1").Value = Variable1
    
        Msgbox Variable1 
    
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi bacter!
    No way to do it inside the standard module, but
    there are two other ways.
    Please refer to the attachments.

    --Okami
    Attached Files Attached Files

  10. #10
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    5
    Location
    [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]

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I'm glad you got it working (thanks 大灰狼1976 ), 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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    5
    Location
    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.Image1.jpg


    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
    Attached Files Attached Files

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •