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 16th, 2002, 02:15 PM   #1
DJM1216
Board Regular
 
Join Date: Apr 2002
Location: Delray Beach, FL
Posts: 77
Default

Hi there, I have never written a macro before and am looking for some help. I want to write an if then formula into a macro. Here is what I need to do (ficticious example)

I am comparing rates being charged to my company which is a function of a certain completion percent.

Say cell "C3" reads 80% (or any percent, can vary monthly)

I need to fill out the rate I am being charged by my vendor. For example pricing would look like this:
Vendor XYZ corp
50-75% $2.22
76-95% $1.95
96-100% $0.85


Now different vendors charge different rates, so I would need this formula for every vendor. Does that all go on one macro, or separate macros, that would be run by a "process" macro?

Basically, I need my final report to say what my completion rate is and because of that, what my vendors will charge me.

Does that make sense?

Thank you in advance.
DJM1216 is offline   Reply With Quote
Old Apr 16th, 2002, 03:53 PM   #2
John McGraw
Board Regular
 
Join Date: Feb 2002
Posts: 76
Default

Quote:
On 2002-04-16 13:15, DJM1216 wrote:
Hi there, I have never written a macro before and am looking for some help. I want to write an if then formula into a macro. Here is what I need to do (ficticious example)
Thank you in advance.
Its a bit hard for me to understand exactly what you want to do. But if I am understanding correctly I think this is what you want to do...

First of all, this function can be used in a worksheet and recieves two arguments. The first is "Comp_Prcnt" which is the percentage completion. (you can use a reference like "a1" or wherever you keep the completion precentage) The second argument is the vendor code... You will need to asign each vendor with a code to which it will be refered. In the example XYZ corp is coded as 1, and ABC corp is 2.

I used a select case structure instead of if, then because it is alot easier in when you have alot of factors to work with. (You could do this with if-then but it would be very hard to read)


Function Rate_Charged(Comp_Prcnt As Double, VendorCode As Integer)
'Declare constants to refer to the vendor codes
Const XYZcorp As Integer = 1
Const ABCcorp As Integer = 2

Select Case VendorCode
Case XYZcorp
Select Case Comp_Prcnt
Case 0.5 To 0.75
Rate_Charged = 2.22
Case 0.76 To 0.95
Rate_Charged = 1.95
Case 0.96 To 1
Rate_Charged = 0.85
End Select
Case ABCcorp
Select Case Comp_Prcnt
Case 0.5 To 0.75
Rate_Charged = 4.8
Case 0.76 To 0.95
Rate_Charged = 3.5
Case 0.96 To 1
Rate_Charged = 2.22
End Select
End Select

End Function




Just enter this function into a formula and it will return the rate charged by the vendor.

You can adjust the code to add more vendors with their respective codes. Just add a new case in the Select Case VendorCode section of the code.





[ This Message was edited by: John McGraw on 2002-04-16 15:06 ]
John McGraw is offline   Reply With Quote
Old Apr 17th, 2002, 02:22 PM   #3
DJM1216
Board Regular
 
Join Date: Apr 2002
Location: Delray Beach, FL
Posts: 77
Default

Where you're putting in "Vendorcode" is that the column heading, or is that where I put in the cell reference?

I entered this macro, but no results.

Also when I start a new macro it starts with something like "Sub Macro ()", what is that for?

Thanks.
DJM1216 is offline   Reply With Quote
Old Apr 18th, 2002, 01:16 PM   #4
John McGraw
Board Regular
 
Join Date: Feb 2002
Posts: 76
Default

Quote:
On 2002-04-17 13:22, DJM1216 wrote:
Where you're putting in "Vendorcode" is that the column heading, or is that where I put in the cell reference?

I entered this macro, but no results.

Also when I start a new macro it starts with something like "Sub Macro ()", what is that for?

Thanks.
Ok, you need to paste this function into a module. Do this:

Press ALT+F11
Choose the "Insert" Menu
Choose "Module"

Now an empty module will pop up. Select the code I wrote for you in the above message and copy it, then paste it into the new VBA module.

In excel you can now enter this function just like you would any other function. Do the following as a test:

In cell A1 enter: "80%"
in cell B1 enter: "=Rate_Charged(A1,1)"

In the formula "=Rate_Charged(a1,1)", the first argument is the %completion or A1, the second argument is the vendor code. (1 in this example, or XYZcorp) If you would have entered: "=Rate_Charged(a1,2)" that would have given you the rate charged by ABC corp.

It seems you are very new to VBA. Check out the following to get some basic understanding http://www.mrexcel.com/tip038.shtml


Also for more articles on excel and VBA try:

http://www.mrexcel.com/index.html#past

Especialy check out the sections "VBA Macros - Introduction" and "VBA Macros - Examples"

I hope that helps. If you still cant figure it out, let me know. Dont try to record a macro, you just need to create a new module and dump the code into it.




[ This Message was edited by: John McGraw on 2002-04-18 12:23 ]
John McGraw 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 08:01 AM.


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