LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Code Contribution

I use UserForms almost exclusively for supplying input data to my
Excel programs. I permit users to enter the numerical data in the
respective textboxes in virtually any formatted or algebraic form,
such as $12,345,678 or $12.345*(1.035)^2.5-10500/2

The ability to modify the data algebraically lets users pose
"What If?" questions without having to find their hand
calculators and avoids transcription errors. I also wanted
to trap an incorrect symbol that is inadvertently entered
by the user.

Several MVP's (particularly Melanie B.) supplied insightful
help, and I would like to post the finished procedure for
anyone who's interested. If any of you MVP's believe it
worthy enough, you can pass it up to one of the Excel
web sites that catalog novel procedures.

-- Dennis Eisen

In the code for each texbox insert:

Private Sub MyTextBox_AfterUpdate()
Dim ResultString As Variant
ResultString = Algebra(MyUserForm.MyTextBox.Text)
MyUserForm.MyTextBox.Text = Format(ResultString, "$###,###,##0")
'Replace $###,###,###0 by whatever format each input requires
End Sub


In the main module code insert:

Function Algebra(InputString As String) As Variant
If InputString Like "*[!0-9$.,+-/*^()]*" Then
'The ! right after [ serves as the negation operator
Dim Message As String
Message = "Sorry, the input field you just left contains one or more illegal
characters."
MsgBox Message, 48, "Data Entry Error"
Algebra = InputString
Exit Function
End If
Algebra = InputString
If InputString Like "*[+-/*^()]*" Then
InputString = Replace(InputString, "$", "")
InputString = Replace(InputString, ",", "")
Algebra = Evaluate(InputString)
End If
If Algebra = "" Then Algebra = 0
End Function



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to calcuate 6 variable's contribution towards a fixed sum??? Alexander Excel Discussion (Misc queries) 2 April 20th 10 09:46 AM
NPV of cashflows to assess contribution to a joint venture ExcelMonkey Excel Worksheet Functions 2 November 20th 07 12:14 AM
Percent Contribution JR573PUTT Excel Discussion (Misc queries) 2 August 17th 06 06:35 PM
How to calculate pension contribution on salary? pgruening Excel Discussion (Misc queries) 4 September 7th 05 09:28 PM
Question AND my 1st positive contribution rju Excel Programming 1 January 21st 04 05:08 PM


All times are GMT +1. The time now is 03:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"