Entering a function into vba using UserForm

cfolsom

New Member
Joined
Nov 11, 2008
Messages
2
i am using a userform to try and enter a function like (x^2-1) into a text box. I am trying to get my vba program to recognize this as a function so that i can evaluate the function at a specific value that is also entered in a text box in the UserForm. I am kind of new to VBA so any help would be appreciated.
 

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
Well, you'd need to identify "x" in some way, probably as a numeric variable.

Let's say you have your posted expression of
x^2-1
in a textbox named TextBox1 on your userform.

You'll of course need to assign some association between the "x" and a numeric value, so for demonstration purposes, let's define "x" as 8 by using a Double type variable, with this example that evaluates the expression by clicking a command button named CommandButton1.

Code:
Private Sub CommandButton1_Click()
Dim xVal#, xString$
xVal = 8
xString = WorksheetFunction.Substitute(TextBox1.Text, "x", xVal)
MsgBox Evaluate(xString), , "Answer:"
End Sub


The answer in the message box is 63 because we assigned 8 to x, therefore
x^2-1
equals
8^2-1
equals
64-1
equals
63
 
Upvote 0
If you are directing that comment at me, I'd contend there is still no problem because the workbook author would know to accept or not accept that expression, and if accepted, allow for the superscript to be represented in a recognizable way in the textbox entry. This is actually a simple problem solved as I exampled, made simpler by the workbook author defining what is OK and not OK and pre-coding the solution to fit how they allow the expression to be entered.
 
Upvote 0
Based on Tom's excellent solution here is a small 'bells and whistles' suggestion:

Let's say the userform has two textboxes TBFunc and TBValue for the function and value respectively and a label LblResult to display the result.

Use something like (code to be pasted in the code module for the user form):

Code:
Private Sub TBFunc_AfterUpdate()
  EvaluateFunction
End Sub
 
Private Sub TBValue_AfterUpdate()
  EvaluateFunction
End Sub
 
Private Sub EvaluateFunction()
  LblResult = ""
  xString = WorksheetFunction.Substitute(TBFunc.Text, "x", TBValue)
  On Error Resume Next
  LblResult.Caption = Evaluate(xString)
  On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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
Back
Top