0

In the last 30 minutes I am trying to execute a string in VBA as a command. The command is like this and it runs ok:

activesheet.chb_g_ba1.visible = true

What I am trying to do is to set "chb_g_ba1" as a variable, because there are "chb_g_ba2", "chb_g_ba3", etc as well.

What I have tried so far - things like:

dim l_counter as long: l_counter = 1
Evaluate (CStr("me.chb_g_ba" & l_counter & ".visible = true"))

Or even Eval

Eval (CStr("me.chb_g_ba" & l_counter & ".visible = true"))

Eval is a function in MS Access VBA, but obviously it is not present in Excel. Pretty much my question is extremely similar to this one:VBA how to run a string as a line of code, but it is for Excel, not for Access.

So, any ideas are welcomed! :)

7
  • 2
    Assuming that's a control on the sheet, use: activesheet.oleobjects("chb_g_ba1").visible = true Commented Apr 13, 2016 at 10:28
  • 1
    No, other than actually writing the code to a module. Commented Apr 13, 2016 at 10:31
  • Ok, thank you. I have deleted my answer just to double check it. But it really works. The Question was - "Is it possible to do it like Eval()" Commented Apr 13, 2016 at 10:32
  • 1
    I don't know what answer you are talking about. There is no Eval or equivalent for this in Excel VBA. Nor can I think of a good reason to need one, to be honest. Commented Apr 13, 2016 at 10:36
  • 1
    You need to access the object contained in the OLEobject: activesheet.oleobjects("chb_g_ba1").Object.value = True Commented Apr 13, 2016 at 11:29

1 Answer 1

1

Credit to CPearson:

There is a way to do that but we have to add a new module dynamically with required code and delete while finished.

References: Microsoft Visual Basic for Applications Extensibility 5.3

Sub test()
    Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
    VBComp.Name = "NewModule"
    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("NewModule").CodeModule
    Dim l_counter As Long
    l_counter = 1
    With VBCodeMod
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, _
        "Sub MyNewProcedure()" & Chr(13) & "UserForm1.chb_g_ba" & l_counter & ".Visible = True" & Chr(13) & "End Sub"
    End With
    'run the new module
    Application.Run "MyNewProcedure"
    UserForm1.Show
    'Delete the created module
    ThisWorkbook.VBProject.VBComponents.Remove VBComp
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

It also requires trusted access to the vb project, which is not set by default.
Thanks, Karthick. But it looks like overkill. I just wanted to eliminate writing 10 lines of code.
you can add more lines by using the chr(13)
I mean, the whole idea was not write If Else 10 times. But thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.