Value fo Variables and MSScriptControl

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Good morning all,

At the moment I have a form with lots of textboxes, named in a coherent fashion. These relate both to a string variable containing the cell references on a sheet and a double variable conatining the values.

For instance.

the textbox me.txtSHCV relates to varSHCV and varSHCVt.

I have a caluculation that determines the name of the other variables based on the name of the text box.

i.e.

OptName = OptionBtn.Name
NName = Mid(OptName, 4, Len(OptName) - 2)
OptName = CStr("var" & NName & "")
OptName2 = CStr("var" & NName & "t")

Where OptName would be varSHCV and OptName2 would be varSHCVt.

OK, so why I hear you say. Well I am using MSScript control to change the values of these variables when ever a text box is updated.

So:

Rich (BB code):
Dim Script_Control As MSScriptControl.ScriptControl, Script_Control2 As MSScriptControl.ScriptControl
 
Set Script_Control = New MSScriptControl.ScriptControl
Set Script_Control2 = New MSScriptControl.ScriptControl
 
With Script_Control
    .Language = "VBScript"
    .AddObject "WsVB", Workbooks(1).Sheets("VBA Data")
    .AddCode _
        "Sub ChangeVals" & vbCrLf & "" & varSHCVt & " = " & CoVal & "" & vbCrLf & "End Sub"
    .Run ("ChangeVals")
End With

The above (sucessfully) changes the value of the reference cell in the reference sheet. Alternitively, I have a similar peice of code changes the value of the highlighted variable (varSHCV).

My issue is that I am struggling to pass the caluclated name of the variable into the Script.

My question is, is there anyway you can think of doing this?

For instance, OptName = varSHCVt, varSHCVt = Cells(1,1).Value.

I would like a function that would calculate the above so that NewVar = Cells(1,1).Value.

Obviously there are other ways I could do this, but in an effort to learn about MSScriptControl (and to avoid making my work over the last day meaningless) I would liek to continue with this methodaology if possible.

Many thanks in advance
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
I've tried to get around this issue by using a function:

Code:
With Script_Control2
.Language = "VBScript"
.AddCode "Function DetermineVals()" & vbCrLf & "DetermineVals = " & OptName2 & " " & vbCrLf & "End Function"
End With
rslt = Script_Control2.Eval("DetermineVals")

When you debug.print this statement, you get

Code:
Function DetermineVals()
DetermineVals =  varSHCVt
End Function

However, the function returbs rslt as empty. Any ideas?
 

Watch MrExcel Video

Forum statistics

Threads
1,122,519
Messages
5,596,635
Members
414,083
Latest member
Mrsash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top