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 20th, 2002, 08:51 AM   #1
rferwerda
New Member
 
Join Date: Mar 2002
Posts: 4
Default

I'm new to Excel VBA macros, and I'm kinda stuck on where to proceed next. I'm attempting to present the end user with small UserForm (so far, this works) that gives them the opportunity to make a choice from a ComboBox. After the choice is made, and the Command button on the UserForm is clicked, I'd like to have the selected data be sent to the active cell (actually, a cell two cells to the right, relative). Making the choice go to the cell is the part not yet working.

What I've got so far:

UserForm:
=========
Private Sub CommandButton1_Click()

End Sub

Private Sub UserForm_Initialize()
Dim MyArray As Variant
Dim i As Integer

'Initialize array with values to populate ComboBox.
MyArray = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")

For i = LBound(MyArray) To UBound(MyArray)

'Add a value from MyArray to ComboBox1.
UserForm1.ComboBox1.AddItem MyArray(i)

Next
End Sub

Private Sub UserForm_Click()

End Sub

========================================

(this UserForm is called from the following Macro code - the macro selects the highlighted cell on one sheet, switches to another sheet, and pastes the contents into the current active cell on that sheet. Then, the UserForm is presented - I'd like to be able to complete the task by having the UserForm's selection from the ComboBox then be pasted into the cell two positions to the right of the active cell):

Sub Pick()

ActiveCell.Select
Selection.Copy
Sheets("Sheet2").Select
ActiveCell.Offset(0, 0).Range("A1").Select
ActiveSheet.Paste
UserForm1.Show
End Sub

============================================

Any help in getting this step figured out would be appreciated. Thank you all!

-Bob
rferwerda is offline   Reply With Quote
Old Mar 20th, 2002, 09:03 AM   #2
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi Bob

Hope you don't mind me saying, but you are over-complicating things.

Try:

Private Sub CommandButton1_Click()
Sheet2.Range("A1") = ComboBox1
ComboBox1 = vbNullString
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer

For i = 1 To 10
Me.ComboBox1.AddItem i
Next i

End Sub
Dave Hawley is offline   Reply With Quote
Old Mar 20th, 2002, 09:06 AM   #3
smozgur
BatCoder
 
Join Date: Feb 2002
Location: Turkey
Posts: 764
Default

This code would send the combobox selected value into the A1 cell in Sheet1.

Private Sub CommandButton1_Click()
Sheet1.Cells(1,1)=Combobox1.Text
End Sub

is that ok?


smozgur is offline   Reply With Quote
Old Mar 20th, 2002, 09:06 AM   #4
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

You could probably just use:

Sub Pick()
ActiveCell.Copy Destination:= _
Sheets("Sheet2").Range("A1")
UserForm1.Show
End Sub

To launch your UserForm. Try to avoid relying on Active cells and Selections and work with them directly. Rarely a need to Select or Activate.


Dave Hawley is offline   Reply With Quote
Old Mar 20th, 2002, 09:14 AM   #5
Sbirro Cornuto
Board Regular
 
Join Date: Mar 2002
Location: Little Italy
Posts: 93
Default

Why not use ComboBox.ControlSource property directly?
Sbirro Cornuto is offline   Reply With Quote
Old Mar 20th, 2002, 09:43 AM   #6
rferwerda
New Member
 
Join Date: Mar 2002
Posts: 4
Default

Dave, smozgur,

Thanks for the help - I believe I'm on the right track. However, I'm now getting an error when clicking on the CommandButton to send the choice to the sheet:

Run time error '424'

Object required.

This error points to the statement:

Sheet2.Cells(1,1) = ComboBox1.Text

or

Sheet2.Range("A1") = ComboBox1

Any ideas what I'm missing? Thanks!

-Bob
rferwerda is offline   Reply With Quote
Old Mar 20th, 2002, 01:11 PM   #7
smozgur
BatCoder
 
Join Date: Feb 2002
Location: Turkey
Posts: 764
Default

I think :

Sheet2 object name is not "Sheet2" in your project

or

ComboBox1 object name is not "ComboBox1" in your UserForm

Who knows? Error just says that. May be you need full reference for those objects:

Workbookname.Sheet2....=UserFormName.Combobox1....

Here : Sheet2 is not the name you see in label, it is the object name which you see in project window. If you want to use sheet name then you should use:

Sheets("Sheet2").....

May be helps.

smozgur is offline   Reply With Quote
Old Mar 20th, 2002, 03:53 PM   #8
rferwerda
New Member
 
Join Date: Mar 2002
Posts: 4
Default

Smozgur,

Thanks - I believe that corrected the problem. The only thing not currently working is the cell to which the combobox data is being sent. The premise here is to already have an active cell (the choice can change, but the choice is made BEFORE the UserForm is called), and then send the combobox selection to a cell offset two cells to the right. This is what I'm currently using (remember, beginner here, no snickerring ):

Private Sub CommandButton1_Click()
Worksheets("MyTeam").Activate
ActiveCell.Offset(0, 2).Range("A1").Select
Sheets("MyTeam").Range("A1") = UserForm1.ComboBox1.Value
UserForm1.ComboBox1.Value = vbNullString
UserForm_Click

End Sub

Private Sub UserForm_Click()
Unload Me

End Sub

============================================

The value froom the combobox is ending up in cell R1, C1. Where's my mistake?

Thanks!

-Bob
rferwerda is offline   Reply With Quote
Old Mar 20th, 2002, 04:02 PM   #9
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

):

Private Sub CommandButton1_Click()
Worksheets("MyTeam").Activate
ActiveCell.Offset(0, 2).Range("A1").Select
activecell.value= UserForm1.ComboBox1.Value
UserForm1.ComboBox1.Value = vbNullString
UserForm_Click

End Sub

Private Sub UserForm_Click()
Unload Me

End Sub

Al Chara is offline   Reply With Quote
Old Mar 21st, 2002, 09:19 AM   #10
rferwerda
New Member
 
Join Date: Mar 2002
Posts: 4
Default

Al Chara,

Thank you - that did the trick!

And, thank all of you for your help! I've learned alot!

-Bob
rferwerda 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:02 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