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 May 8th, 2002, 06:07 PM   #1
TheMatrixReloaded
New Member
 
Join Date: May 2002
Location: Baltimore
Posts: 29
Default

I know there's a way to make these If statements into one With statement or Select statement. If you can help, it will be greatly appreciated. Here is my code.

Private Sub cmdNext_Click()
'**********************************************************
'* Variables are dimensioned for use. *
'* ********************************************************
Dim intcurrentrow As Long, lngcurrentvalue As Long, bytToppings As Byte
Dim strMessageString As String
Dim bytAnotherPizza As Byte
Dim i As Integer
Dim x As Byte
Dim z As Byte
Me.Hide
intcurrentrow = Sheets("Order Table").[a65536].End(xlUp).Offset(1).Row
Sheets("Order Table").Range("A" & intcurrentrow).Value = Time
Sheets("Order Table").Range("B" & intcurrentrow) = myord
For t = 0 To lstToppings.ListCount - 1
If lstToppings.Selected(t) Then
bytToppings = bytToppings + 1
End If
Next t
'**********************************************************
'* In this section several if statements are used to *
'* place the type and price of the pizza into the *
'* corresponding cells on the Order Table. *
'**********************************************************
If OPTPan.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "Personal"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 4.99 + 0.5 * bytToppings
End If
If OPT10inch.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "10 inch"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 5.99 + 0.5 * bytToppings
End If

If OPT12inch.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "12 inch"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 7.99 + 0.5 * bytToppings
End If

If OPT14inch.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "14 inch"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 9.99 + 0.5 * bytToppings
End If

If OPT16inch.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "16 inch"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 10.99 + 0.5 * bytToppings
End If
If OPTSicilian.Value = True Then
Sheets("Order Table").Range("C" & intcurrentrow).Value = "Sicilian"
Sheets("Order Table").Range("D" & intcurrentrow).Value = 11.5 + 0.5 * bytToppings
End If
txtPhoneNumber = Empty
TxtAddress = Empty
Me.lstToppings.Clear
frmOrderType.cboOrderType.Clear
Call PopulateOrderType
For x = 0 To 3
frmOrderType.cboOrderType.AddItem astrOrderType(x)
Next x
frmOrderType.cboOrderType.Width = 90
Application.Run ("popTopper")
For z = 0 To 7
lstToppings.AddItem astrToppings(z)
Next z
Me.lstToppings.Visible = True
'**********************************************************
'* This section simply asks the customer if they would *
'* like to order another pizza. If yes, the order form *
'* is displayed, if no all fields are cleared for the *
'* order. *
'**********************************************************
strMessageString = "Your pizza will be done soon!"
strMessageString = strMessageString & Chr(13)
strMessageString = strMessageString & "Do you want to order another?"
bytAnotherPizza = MsgBox(strMessageString, vbYesNo, "Another?")
If bytAnotherPizza = vbNo Then
Else:
OPTPan = False
OPT10inch = False
OPT12inch = False
OPT14inch = False
OPT16inch = False
OPTSicilian = False
frmOrderType.Show
End If
End Sub
TheMatrixReloaded is offline   Reply With Quote
Old May 8th, 2002, 06:32 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

The code I provided last night *should* work very nicely (if tweaked properly).

Here it is with the variable set to True and False rather than 1 and 0.

Code:
Sub test()
Dim MyArray, x, y, z
Dim OPTPan, OPT10inch, OPT12inch
Dim OPT14inch, OPT16inch, OPTSicilian

''''''''''''''''''
' these are given, placed here for testing
OPTPan = False
OPT10inch = False
OPT12inch = False
OPT14inch = False
OPT16inch = True
OPTSicilian = False
''''''''''''''''''''''''''''
''''''''''''''''''''''''''''

MyArray = Array(OPTPan, OPT10inch, OPT12inch, OPT14inch, OPT16inch, OPTSicilian)
x = Application.Match(True, MyArray, 0)
y = WorksheetFunction.Choose(x, "Personal", "10 inch", "12 inch", "14 inch", "16 inch", "Sicilian")
z = WorksheetFunction.Choose(x, 4.99, 5.99, 7.99, 9.99, 10.99, 11.5)

With Sheets("Order Table")
    .Range("C" & intcurrentrow) = y
    .Range("D" & intcurrentrow) = z + 0.5 * bytToppings
End With

End Sub
I encourage you to use this, or a variant of this, for efficiency. Zero if statements.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 8th, 2002, 06:39 PM   #3
James
Board Regular
 
Join Date: Feb 2002
Location: Huntington Beach, CA USA
Posts: 327
Default

Hi Jay,

I like it! I need to study this and start going in this direction. Those "If" statements drive me crazy.

James
James is offline   Reply With Quote
Old May 8th, 2002, 06:53 PM   #4
TheMatrixReloaded
New Member
 
Join Date: May 2002
Location: Baltimore
Posts: 29
Default

so that code should work? I'm gonna try it, I'll let ya know man. Thanks
TheMatrixReloaded is offline   Reply With Quote
Old May 8th, 2002, 10:27 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

Just to test that it will work, copy the code posted to a new module. Comment out the code where you write to the worksheet.

Make your last bit of code look like this instead.

''''''''''''''''''''''''
MyArray = Array(OPTPan, OPT10inch, OPT12inch, OPT14inch, OPT16inch, OPTSicilian)
x = Application.Match(True, MyArray, 0)
y = WorksheetFunction.Choose(x, "Personal", "10 inch", "12 inch", "14 inch", "16 inch", "Sicilian")
z = WorksheetFunction.Choose(x, 4.99, 5.99, 7.99, 9.99, 10.99, 11.5)

MsgBox x
MsgBox y
MsgBox z
'''''''''''''''''''

And run the sub. The three message boxes should tell you that you have captured the button values correctly (when adjusted to fit the actual file), and that you can do a lot more with these techniques.

An additional note:

Using MATCH in VBA is tricky.

WorksheetFunction.Match rarely works as it should. Application.WorksheetFunction.Match has the same problems.

Always use only Application.Match, as it is stable and not quirky.
Jay Petrulis 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 12:34 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