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 14th, 2002, 04:25 AM   #1
neilb
Board Regular
 
Join Date: Apr 2002
Posts: 128
Default

Hi

I have just strated trying to use VBA.

I am currently trying to use a list box in a userform. I have created a listbox with a list of choices in.(this has been done by using the property window and rowsource). Once a choice is selected in the listbox how do I get a linked different worksheet to open?

Many thanks in advance

Neil
neilb is offline   Reply With Quote
Old May 14th, 2002, 04:31 AM   #2
Audiojoe
Board Regular
 
Join Date: Feb 2002
Location: Where the wild roses grow
Posts: 285
Default


It'd be easier if you could do the listbox in VBA as part of a userform. Do you know how to do this, or do you need some help?
Audiojoe is offline   Reply With Quote
Old May 14th, 2002, 04:45 AM   #3
neilb
Board Regular
 
Join Date: Apr 2002
Posts: 128
Default

No

I don't know how to do that. I am just begining to use vba


neilb is offline   Reply With Quote
Old May 14th, 2002, 05:02 AM   #4
Audiojoe
Board Regular
 
Join Date: Feb 2002
Location: Where the wild roses grow
Posts: 285
Default


OK, just try this as an exercise in a new workbook and adapt it however you like.

First, click on VIEW>TOOLBARS>CONTROL TOOLBOX, and select the button icon, then draw yourself a button to trigger the listbox for now. You can decide on other ways of triggering it later, such as when you enter a certain cell, or when you open a certain sheet. Once you've drawn the button, double click on it. This takes you to the VB Editor

It should say this:

Private Sub CommandButton1_Click()

End Sub


Click in the line between the two and write:

Userform1.Show

so now it should look like this:

Private Sub CommandButton1_Click()
Userform1.Show
End Sub

Now click on INSERT>USERFORM and a blank userform will appear along with a toolbox. Select the Listbox icon from the toolbox and draw it onto the userform. When you have finished, select the command button icon and draw one of those on there as well.

OK, now you want to add some choices to your listbox, so double click on the userform itself, and this should appear:

Private Sub UserForm_Click()

End Sub

You need to change the word Click to Initialize. This is telling the userform what to do when it initializes. In the lines between the two again, type the following:

Listbox1.AddItem "A"
Listbox1.AddItem "B"
Listbox1.AddItem "C"

so now it should look like this:

Private Sub UserForm_Initialize()
Listbox1.AddItem "A"
Listbox1.AddItem "B"
Listbox1.AddItem "C"
End Sub

Now to get back to your userform, double click on Userform1 in the upper left hand side window. When you get back, double click the button you drew. You'll have this:

Private Sub CommandButton1_Click()

End Sub

Put in the following so it looks like this:

Private Sub CommandButton1_Click()
Worksheets("Sheet1").range("a1").value=listbox1
Unload Me
Sheets("sheet2").select
End Sub

This tells it that once you click the button it enters what you chose from the list into cell A1 on Sheet1. Then it opens another sheet. You can change this to whatever sheet it is you want to open

If you want more help, just shout me

Audiojoe
I was in nappies when I started writing this
Audiojoe is offline   Reply With Quote
Old May 14th, 2002, 05:33 AM   #5
neilb
Board Regular
 
Join Date: Apr 2002
Posts: 128
Default

Nice one - top bloke

Just one question ... Do I use a IF statement to get it to go the the right sheet depending on the choices?( there is 15 sheets for them to choose from)

Is it easily possible to get this userform to open when the spreadsheet opens

Neil
neilb is offline   Reply With Quote
Old May 14th, 2002, 05:43 AM   #6
Audiojoe
Board Regular
 
Join Date: Feb 2002
Location: Where the wild roses grow
Posts: 285
Default


Yes, if for example when you choose "A" from the listbox you want to go to Sheet3, then use this in the command button's click event:

If Listbox1.value = "A" then
Unload Me
Sheets("sheet3").select
End if

and so on...

Audiojoe
Audiojoe is offline   Reply With Quote
Old May 14th, 2002, 06:23 AM   #7
neilb
Board Regular
 
Join Date: Apr 2002
Posts: 128
Default

Excellent

Is it easy to get a userform to open when the workbook is opened?

Neil
neilb is offline   Reply With Quote
Old May 14th, 2002, 06:29 AM   #8
Audiojoe
Board Regular
 
Join Date: Feb 2002
Location: Where the wild roses grow
Posts: 285
Default


Yeah, just click on a cell, then press ALT+F11. When the VB Editor comes up, look in the upper left hand window and double click on This Workbook (if you have two or more open, you will have to find the right one!).

Now you should have a blank sheet. There are two little drop down boxes above it. From the left one select Workbook and from the right one select Open.

Now you should have this:

Private Sub Workbook_Open()

End Sub

In it you need to put:

Userform1.Show

So it looks like this:

Private Sub Workbook_Open()
Userform1.Show
End Sub


Audiojoe
Kumbayah to you all
Audiojoe 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 07:29 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