Run a statement in a variable RRS feed

  • Question

  • I have the following in a text file (so I can change it in text file without changing VB code)

    X = 1

    Then I imported "X = 1" into a variable Y so now I got Y = "X = 1".

    How do I run this assignment in Y so I will get variable X created with 1 in it for later use?

    Tuesday, August 27, 2013 1:03 AM

All replies

  • Since no one has answered yet I'll post a thought.  I don't know how you "imported x=1 into y", but leaving that aside, it sounds as if you are going to need to write code that changes code.

    Look at Bernie's comment about the VBA Project Model and then you'll have to research the more detailed information in the MSDN library, and elsewhere on the web, to do exactly what you want to do.

    I've used this method to create worksheets with event code and forms "on the fly", but not for the purpose you describe.


    Tuesday, August 27, 2013 12:31 PM
  • Sorry, I try to make it clear:

    I have the following powershell code working and looking for VB code with the same function:

    $a = '$b = 123'                           #assign a string "$b = 123" to a variable $a

    Invoke-Expression $a                 #execute the expression inside $a

    write-output $a                           #display $a content

    $b = 123                                     #$a content is shown as a string "$b = 123"

    write-output $b                           #display $b content

    123                                              #$b content is shown as a number 123

    • Edited by Ian3 Tuesday, August 27, 2013 3:09 PM
    Tuesday, August 27, 2013 3:07 PM
  • I don't believe the equivalent exists.

    You'd have to parse the expression and write code that modifies the VBA module


    Tuesday, August 27, 2013 5:29 PM
  • Thanks Ron. Do you have info how to do that?

    Tuesday, August 27, 2013 11:41 PM
  • Well, start with Chip Pearson's site page on Programming the VBA Editor.

    So far as parsing the expression, if it is always going to be in the format of {variable} = {constant}  then you can proably break it apart using the Split function.  e.g.

    Const Y = "x=1"
    Dim temp As Variant
    temp = Split(Y, "=")

    temp(0) would then contain x
    temp(1) would contain 1


    Wednesday, August 28, 2013 12:34 AM