If statement - visual basic for an input form

emily2309

Board Regular
Joined
Nov 17, 2009
Messages
51
I am creating a user input form where the user can enter data about what they have eaten on a certain day. I am using radio buttons, so when the radio button has been clicked, it automatically puts something into cell A1. What has been put into cell A1 will then be used to say what is in the combo box named Cbo_FoodItem. I have used the following code:

With Me.Cbo_FoodItem
If Worksheet("na").Range("A1") = "carbs" Then
Cbo_FoodItem.List = Worksheet("foodtable").Range("B2:B215").Value
End If
End With

However, when i try to test it, i get an error which then highlights the "Worksheet" after IF and says "Sub or Function not defined".
Obviously there is something wrong with the formulae, however i cannot work it out. Please help.
I've only just started using visual basic, but i am starting to get the gist of it.
Thanks :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
A oneliner will do

Code:
If Thisworkbook.sheets("na").Range("A1") = "carbs" Then Cbo_FoodItem.List = Thisworkbook.sheets("foodtable").Range("B2:B215")
 
Upvote 0
A one letter will do - it should be Worksheets not Worksheet.:)
 
Upvote 0
@Norie

filling a combobox with

Code:
Cbo_FoodItem.List = Worksheet("foodtable").Range("B2:B215[COLOR=red]").Value[/COLOR]
????
 
Upvote 0
Sorry I don't know what you mean.:unsure:

That code is also missing the vital s.:)
 
Upvote 0
I worked out the s, sorry about that lol.
Now i have another problem, it's telling me that it has run time error 9 and highlights the following:


Sub Food_Input_open()
Food_Input.Show
End Sub
 
Upvote 0
Emily

Goto Tools>Options and check Break on All Errors on the General tab.

Now when you run the code and get that error you should be able to find out what the problem is.

It's most likely to be something in the userform's Initialize event code, if you have any of course.:)
 
Upvote 0
Thank you. It's now telling me the issue is again with the IF statement:

With Me.Cbo_FoodItem
If Worksheets("na").Range("A1") = "carbs" Then
Cbo_FoodItem.List = Worksheets("foodtable").Range("B2:B215").Value
End If

I don't know what is wrong with this.
 
Upvote 0
Do you have a worksheet called 'foodtable'?

Do you have a worksheet called 'na'?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top