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 9th, 2002, 08:37 AM   #1
Q
Board Regular
 
Join Date: Apr 2002
Location: 20 Minutes outside Manhattan
Posts: 64
Default

I have been skulking anonymously around this board(s) for a few months now. I found it such an incredibly useful and helpful site that I decided to join a few weeks ago. Every member should take pride in this board; there are very few sites that are this "friendly" and useful. BRAVO MR. Excel and Juan.
Alright then all *** kissing aside here’s my question.
I have written an Else If statement for a command button on a userform I created. This form basically has 2 Input fields; one combo box allows the user to choose a variable, the variable being 1 to 90 and S1 to S44. The other field is a date, which is chosen off a calendar control.
Each variable from the combobox field is what is keyed on. For example if S33 is chosen the date field is copied to a sheet called "S33", a row is inserted on this sheet, and then the date is also copied to a sheet called "Premium". So each respective variable copies a date to its own personal sheet and also to one of three other sheets. (One of three sheets meaning that the numbers are broken up into different groups on different sheets. 1 through 55 on one sheet and S1 through S44 on another sheet and so on.) I'm sure there is a better way to write this but I can't seem to do it and have it work consistently. It seems I have become the King of IF statements. I have included a sample below.

If cboTankNumber.Value = "" Then
MsgBox "You must enter a Tank Number"
ElseIf cboTankNumber.Value = 1 Then
Worksheets("Green Fass").Range("D7") = calInput.Value
Worksheets("Tank1").Range("A22") = calInput.Value
Worksheets("Tank1").Rows("22:22").Insert Shift:=xlDown
frmInput.Hide
ElseIf cboTankNumber.Value = 2 Then
Worksheets("Green Fass").Range("F7") = calInput.Value
Worksheets("Tank2").Range("A22") = calInput.Value
Worksheets("Tank2").Rows("22:22").Insert Shift:=xlDown
frmInput.Hide
ElseIf cboTankNumber.Value = 3 Then
Worksheets("Green Fass").Range("H7") = calInput.Value
Worksheets("Tank3").Range("A22") = calInput.Value
Worksheets("Tank3").Rows("22:22").Insert Shift:=xlDown
frmInput.Hide

////////////

ElseIf cboTankNumber.Value = "S43" Then
Worksheets("Schoene").Range("V40") = calInput.Value
Worksheets("TankS43").Range("A22") = calInput.Value
Worksheets("TankS43").Rows("22:22").Insert Shift:=xlDown
frmInput.Hide
Else
cboTankNumber.Value = "S44"
Worksheets("Schoene").Range("X40") = calInput.Value
Worksheets("TankS44").Range("A22") = calInput.Value
Worksheets("TankS44").Rows("22:22").Insert Shift:=xlDown
frmInput.Hide
End If
.........and so on

Any help or suggestions will greatly be appreciated.
Q is offline   Reply With Quote
Old May 9th, 2002, 08:51 AM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

I had a quick glance at this one.

1. Have a look at becoming the King of "Select Case". (examples on this board and in the Help file)

2. These lines of code are in every Else If section:


Worksheets("Tank1").Range("A22") = calInput.Value
Worksheets("Tank1").Rows("22:22").Insert Shift:=xlDown


If you do this for every condition, put this after your "End If" (when you finally hit it). This will clean up your code because you only need to use this code once.

HTH
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old May 9th, 2002, 08:56 AM   #3
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Also, with this part of the code in the first section of Else If's


ElseIf cboTankNumber.Value = 1 Then
Worksheets("Green Fass").Range("D7") = calInput.Value


you can probably replace every single Else If with this:


Worksheets("Green Fass").Range("C7").OffSet(0,cboTankNumber.Value) = calInput.Value


Because the column the "calInput.Value" goes in depends on the number in the combobox cboTankNumber. I'm assuming that this is a pattern.

(PS, glad you've stopped lurking. It took me a while before I started posting here as well)

__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old May 9th, 2002, 08:59 AM   #4
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default

I see there have been some other posts, but since I spent a few minutes on mine, I'll add it for your enjoyment.

I assume sheet "Green Fass" has columns of data
beginning with D7 through some final value with
data every other column. You could do it all
in one statement if you get it right. My example
may not be exactly correct since I cannot see your
workbook, but it should be a good starting point.


dim strTemp as string
dim intTemp as integer

If cboTankNumber.Value = "" Then
MsgBox "You must enter a Tank Number"
Else

strTemp = cboTankNumber.Value
intTemp = cboTankNumber.ListIndex

Worksheets("Green Fass").Cells(7,4 + intTemp*2).value = calInput.Value
Worksheets("Tank" & strTemp).Range("A22").value = calInput.Value
Worksheets("Tank" & strTemp).Rows("22:22").Insert Shift:=xlDown
frmInput.Hide

End If

Note that the listindex of your combobox begins
at 0, so the cell you want to enter the new data
in is Cells(7, 4 + ListIndex*2). This corresponds
to:

Cells(7, 4+0*2) (D7)
Cells(7, 4+1*2) (F7)
Cells(7, 4+2*2) (G7)

etc.

Hope this helps,

K

P.S. Try it out on a practice sheet first!!!
kkknie is offline   Reply With Quote
Old May 9th, 2002, 09:28 AM   #5
Q
Board Regular
 
Join Date: Apr 2002
Location: 20 Minutes outside Manhattan
Posts: 64
Default

Thanks to you both for the quick responses. Mark I will look into Select Case suggestion. In your first response, point 2 I beleive you implied that the lines:
Worksheets("Tank1").Range("A22") = calInput.Value
Worksheets("Tank1").Rows("22:22").Insert Shift:=xlDown

are used through out the staetment. There is a slight variation in each of the Else IF statements. Where "Tank1" is replaced with "Tank2" and so on. So unfortunatly I have to use those lines in each Else If.

kkknie, Thanks and I will try it on a practice sheet. I always do it saves on the heartache.

[ This Message was edited by: Q on 2002-05-09 08:37 ]
Q is offline   Reply With Quote
Old May 9th, 2002, 09:37 AM   #6
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

I did not see that.

How about using something like:


Worksheets("Tank" & cboTankNumber.Value).Range("A22") = calInput.Value


to reference the worksheet.

EDIT:: Oops, sort of similar to what kkknie posted. Apologies kkknie.
_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-05-09 08:38 ]
Mark O'Brien is offline   Reply With Quote
Old May 9th, 2002, 09:42 AM   #7
Q
Board Regular
 
Join Date: Apr 2002
Location: 20 Minutes outside Manhattan
Posts: 64
Default

Yes. Both you and kkknie are on the same path. I'll take that as an omen, it must be a good one to follow.
Thanks to you both for helping cut down the fat.
Q 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 10:25 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