My Nutrition Form/File

Sandler

Board Regular
Joined
Sep 15, 2011
Messages
165
I have a form with a listbox of all my food items. I am trying to get a label to say the specific food items serving size. For example, Almond Milk Serving Size is 1 Cup. Unfortunately, my code is not allowing the label to post the 1 cup aspect of the label. The information would be gathered from this "NData" worksheet. So, it would be
column C &" "& column B. Below the sheet screen capture, I have listed my current code. Please, help.

NData

ABCDEFGHIJK
Cal
Almond MilkCup
AlmondPlus-5XProteincup
Almonds-BD_BToz
Almonds-BD_HDoz
Almonds-BD_CBSoz
Almonds-BD_Strawberryoz
Almonds-CocoaDusted - Nice!cup
Almonds-handfulTJservings
Appleservings

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:282px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:18px;"><col style="width:64px;"><col style="width:18px;"><col style="width:64px;"><col style="width:18px;"><col style="width:64px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #c0c0c0"]P[/TD]

[TD="bgcolor: #c0c0c0"]C[/TD]

[TD="bgcolor: #c0c0c0"]F[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]2.5[/TD]

[TD="align: right"]31[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]5[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]2[/TD]

[TD="align: right"]42[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]5[/TD]

[TD="align: right"]9[/TD]

[TD="align: right"]12[/TD]

[TD="align: right"]164[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]6[/TD]

[TD="align: right"]6[/TD]

[TD="align: right"]15[/TD]

[TD="align: right"]183[/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]6[/TD]

[TD="align: right"]7[/TD]

[TD="align: right"]14[/TD]

[TD="align: right"]178[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]5[/TD]

[TD="align: right"]9[/TD]

[TD="align: right"]12[/TD]

[TD="align: right"]164[/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]

[TD="align: right"]0.25[/TD]

[TD="align: right"]6[/TD]

[TD="align: right"]6[/TD]

[TD="align: right"]12[/TD]

[TD="align: right"]156[/TD]

[TD="bgcolor: #cacaca, align: center"]9[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]7[/TD]

[TD="align: right"]8[/TD]

[TD="align: right"]17[/TD]

[TD="align: right"]213[/TD]

[TD="bgcolor: #cacaca, align: center"]10[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]0[/TD]

[TD="align: right"]15[/TD]

[TD="align: right"]0[/TD]

[TD="align: right"]60[/TD]

</tbody>

Option Explicit

Public servingInfo As String


Private Sub lbxNutrition_Click()

'Variable used to get info from form and function
Dim nutritionMSG As String
Dim cFood As String

'Get the current food selected in the listbox
cFood = Me.lbxNutrition.Value

'Call the GetServing function, passing in the food selected
servingInfo = GetServing(cFood)

' output the content into the label control on serving size of selected food item
nutritionMSG = cFood & " serving size is " & servingInfo
Me.lblNutritionFacts.Caption = nutritionMSG

'activate the controls to be used by the user
Me.lblNutritionFacts.Enabled = True
Me.txtServings.Enabled = True
End Sub

Public Function GetServing(whichFood As String)
'select the Nutrition Data sheet
Worksheets("NData").Range("A1").Select
'loop through column A until correct food item is found
Dim i As Integer
i = 1
Do While ActiveCell.Value <> ""
If ActiveCell.Value = whichFood Then
' with correct food found pass serving info back to calling procedure
servingInfo = ActiveCell.Offset(0, 2).Value & " " & ActiveCell.Offset(0, 1).Value
Exit Do

End If

i = i + 1
Worksheets("NData").Cells(i, 1).Select
Loop
End Function
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I just had to split my Public Function into a separate module and add a header to Range A1 of NData.

This is resolved, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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