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 Mar 2nd, 2002, 09:07 AM   #1
Guest
 
Posts: n/a
Default

I've got a ComboBox ActiveX dropdown control on a worksheet that needs to be initialized with a dropdown list.

What's the best way to do this?

I've tried ComboBox1_GotFocus and ComboBox1_Click without any luck. I've been able to get the list to appear on the second click, but not on the first.

Also tried Worksheet_Activate, but it doesn't seem to work either.

Any suggestions? I know this must be easy.
  Reply With Quote
Old Mar 2nd, 2002, 09:10 AM   #2
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

This code worked for me.

Private Sub ComboBox1_DropButt*******()
Dim i As Integer

ComboBox1.Clear

For i = 1 To 4
ComboBox1.AddItem i
Next i
End Sub

You could also put it in the Workbook_Open() event, or in the Sheet_Activate() event, so it won't have to be recreated each time the user clicks on it.
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old Mar 2nd, 2002, 09:10 AM   #3
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

I mean like this:

Private Sub Workbook_Open()
Dim i As Integer

Sheet1.ComboBox1.Clear

For i = 1 To 4
Sheet1.ComboBox1.AddItem i
Next i
End Sub

__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old Mar 2nd, 2002, 09:59 AM   #4
Matanuska
New Member
 
Join Date: Feb 2002
Location: Houston, Texas
Posts: 28
Default

Thanks!

I'm the Anonymous that asked the original question - I forgot to login. Sorry in advance for the length of this post.

Now I'm able to get the dropdown initialized with a Workbook_Open routine, but this brings up another problem. How do I copy a workbook code module from one workbook to another? (I know how to do this for regular modules - see below).

In my app I create a new workbook with a Sheets(Array("Sheet1", "Sheet2", "Sheet3").Copy method to copy selected sheets out of the master workbook.

This brings any sheet code along with the sheets into the new workbook. I get the module code I want into the new workbook by exporting from the old & importing into the new workbooks using:

ActiveWorkbook.VBProject.VBComponents("modMyModule").Export ("temp1.bas")

Then activate the new workbook & import:

Application.VBE.ActiveVBProject.VBComponents.Import ("temp1.bas")

I tried doing this using "ThisWorkbook" in place of "modMyModule" in the import/export routines above but when I import the workbook code into the new workbook it comes in as a class module rather than as the workbook module.

How do I copy code in "ThisWorkbook" (which contains the Workbook_Open code you helped me out with)?

Matanuska is offline   Reply With Quote
Old Mar 2nd, 2002, 01:59 PM   #5
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Try using this code...I have adapted it to
Juans 1st code...you should adj it to suit.

'here is a routine I did a while ago to do this;
'You will have to change it to reference the correct
'event & sheet
'===================================================================
'= Procedure: Modify_Modules =
'= Type: Subprocedure =
'= =
'= Purpose: Add a Procedure to a module including Sheets and =
'= Thisworkbook =
'= Parameters: None =
'= Returns: Nothing =
'= =
'= Version: Date: Developer: Action: =
'=---------|---------|---------------|-----------------------------=
'= 1.0.0 |13-May-00| Ivan F Moala | Created =
'===================================================================
Sub Modify_Event()

Dim ModEvent As CodeModule 'Module to Modified
Dim LineNum As Long 'Line number in module
Dim SubName As String 'Event to change as text
Dim Proc As String 'Procedure string
Dim EndS As String 'End sub string
Dim Ap As String 'Apostrophe
Dim sTab As String 'Tab
Dim LF As String 'Line feed or carriage return

Ap = Chr(34)
sTab = Chr(9)
LF = Chr(13)
EndS = "End Sub"

'Your Event Procedure OR SubRoutine
SubName = "Private Sub Workbook_Open()" & LF

'Your Procedure
Proc = "Dim i As Integer" & LF
Proc = Proc & _
"Sheet1.ComboBox1.Clear" & LF
Proc = Proc & _
sTab & "For i = 1 To 4" & LF
Proc = Proc & _
sTab & sTab & "Sheet1.ComboBox1.AddItem i" & LF
Proc = Proc & _
sTab & "Next i" & LF

'Use ActiveWorkbook so that it can act on another open/Active workbook
Set ModEvent = ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule

With ModEvent
LineNum = .CountOfLines + 1
.InsertLines LineNum, SubName & Proc & EndS
End With

Set ModEvent = Nothing

End Sub

'Ivan

Ivan F Moala is offline   Reply With Quote
Old Mar 3rd, 2002, 05:47 PM   #6
Matanuska
New Member
 
Join Date: Feb 2002
Location: Houston, Texas
Posts: 28
Default

Thanks. This works well, even if it is a bit cumbersome.

I suppose it might be possible to automate this by writing VBA to export the text of the procedure out the original workbook, parse it into the format you suggest, then insert it as a new line.

Might not even have to parse it, just import as is?

Matanuska 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:44 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