MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2002, 02:16 PM   #1
Ricardo2
New Member
 
Join Date: Apr 2002
Posts: 14
Default

This is my first try at VBA. I have a UserForm with 4 textboxes for a user to input data. I have written a subroutine to use the data and perform some mathematical calculations. What code is needed for the subroutine to get the data entered in the textboxes so they can be used in the calculations? What code do I need in the textbox itself?
Example:
Private Sub TextBox1_Change()
(Code?????)
End Sub
Can I use textboxes to receive numerical data (i.e., numbers)?
I'm new at this so I may not have explained this well. Please let me know if you need more detail.
Ricardo2 is offline   Reply With Quote
Old Apr 9th, 2002, 02:41 PM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
There are a number of ways you could do this.
To perform a calculation, the change event may or may not serve your purpose. Just depends. Just remember, if you use the change event with no further instructions, the code is going to run everytime a single character is entered or removed. Every "change".

If you need the data in all four textboxes before you can perform your calculation, then use a command button for now.


Place your routine in the commandbutton1_click event???

Post all of your code for better help...
Use as much detail as you are willing to give.
Tom

Tom Schreiner is offline   Reply With Quote
Old Apr 10th, 2002, 06:41 AM   #3
Ricardo2
New Member
 
Join Date: Apr 2002
Posts: 14
Default

Tom,

Here is the subroutine code that will run when the CommandButton_click button is clicked:

Sub ImprovedAccrualCalculation()
Dim amount, percentage, fromdate As Date, todate As Date
FormattedAmount = Format(amount, "$##,##0.00")
RevisedPercentage = percentage / 100
FormattedPercentage = Format(RevisedPercentage, "0.000000%")
result = amount * RevisedPercentage * (todate - fromdate + 1) / 365
RoundedResult = Application.WorksheetFunction.Round(result, 2)
FormattedResult = Format(RoundedResult, "$##,##0.00")
accrualdays = todate - fromdate + 1
Msg = FormattedAmount & " x " & FormattedPercentage & " accrued from "
Msg = Msg & fromdate & " to " & todate & " (" & accrualdays & " days)" & " = "
Msg = Msg & FormattedResult
MsgBox (Msg)
End Sub

I tested this code originally using InputBoxes to receive the data and it works fine. I don't know how to assign the value entered in Private Sub TextBox1_Change() (amount), Private Sub TextBox2_Change() (percentage), Private Sub TextBox3_Change() (fromdate), & Private Sub TextBox4_Change() (todate) on the UserFrom and have it carryover into the ImprovedAccrualCalculation subroutine so the calculation can occur.

The code may be a bit "wordy". This is my first attempt.

I appreciate the help.

Ricardo

[ This Message was edited by: Ricardo2 on 2002-04-10 05:43 ]
Ricardo2 is offline   Reply With Quote
Old Apr 10th, 2002, 07:17 AM   #4
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

This is not very pretty either.
Just thrown together...
But it might give you an idea on how to communicate the values from your text boxes....

Just copy this into your userform class module.

Assumes four textboxes and one button named

Textbox1
Textbox2
Textbox3
Textbox4
commandbutton1

Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 10th, 2002, 07:19 AM   #5
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Just to let you know
When Variables are Dim inside a procedure, they are only available to that procedure.
If you put them at the top of the module then they are available to all procedures in the module.

Dim Amount As Currency
Dim Percentage As Double
Dim FromDate As Date
Dim ToDate As Date

Sub ImprovedAccrualCalculation()
FormattedAmount = Format(Amount, "$##,##0.00")
RevisedPercentage = Percentage / 100
FormattedPercentage = Format(RevisedPercentage, "0.000000%")
Result = Amount * RevisedPercentage * (ToDate - FromDate + 1) / 365
RoundedResult = Application.WorksheetFunction.Round(Result, 2)
FormattedResult = Format(RoundedResult, "$##,##0.00")
AccrualDays = ToDate - FromDate + 1
Msg = FormattedAmount & " x " & FormattedPercentage & " accrued from "
Msg = Msg & FromDate & " to " & ToDate & " (" & AccrualDays & " days)" & " = "
Msg = Msg & FormattedResult
MsgBox (Msg)
End Sub

Private Sub CommandButton1_Click()
If Not IsNumeric(TextBox1) Then Exit Sub
'you can place msgbox here to let user know what's up
If Not IsNumeric(TextBox2) Then Exit Sub
If Not IsDate(TextBox3) Then Exit Sub
If Not IsDate(TextBox4) Then Exit Sub
Amount = TextBox1.Value
Percentage = TextBox2.Value
FromDate = TextBox3.Value
ToDate = TextBox4.Value
ImprovedAccrualCalculation
End Sub
Tom Schreiner is offline   Reply With Quote
Old Apr 10th, 2002, 09:02 AM   #6
Ricardo2
New Member
 
Join Date: Apr 2002
Posts: 14
Default

It works!

But I've noticed something. I thought that you had to put subroutines in the Module and put code in UserForm to refer to that subroutine. That's how I had this originally set up. Even after making your changes, the calculation still didn't work. I cut and pasted the ImprovedAccrualCalculation subroutine in Private Sub CommandButton1_click and it works.

One last thing....

I wanted to insert MsgBoxes if the data entered was incorrect. I tried this:

If Not IsNumeric(TextBox1) Then MsgBox "Double check amount"
Exit Sub
If Not IsNumeric(TextBox2) Then MsgBox "Double check percentage"
Exit Sub
If Not IsDate(TextBox3) Then MsgBox "Double check From Date"
Exit Sub
If Not IsDate(TextBox4) Then MsgBox "Double check To Date"
Exit Sub

I tested this and the first MsgBox works. But when I tried with the other three fields, no MsgBox appeared, as though it skips the MsgBox code and goes straight to Exit Sub. What am I missing?

Thanks again.

Ricardo
Ricardo2 is offline   Reply With Quote
Old Apr 11th, 2002, 02:18 PM   #7
Ricardo2
New Member
 
Join Date: Apr 2002
Posts: 14
Default

I figured out how to get around the problem. The code is:

If Not IsNumeric(TextBox1) Then
MsgBox "Double check amount"
Exit Sub
End If
If Not IsNumeric(TextBox2) Then
MsgBox "Double check percentage"
Exit Sub
End If
If Not IsDate(TextBox3) Then
MsgBox "Double check From Date"
Exit Sub
End If
If Not IsDate(TextBox4) Then
MsgBox "Double check To Date"
Exit Sub
End If

Actually, I found the answer in a Dummies book. Hey, at least it works!
Ricardo2 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 06:50 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes