VBA - help creating drop down box from table to insert on another sheet

JTTrickett

New Member
Joined
Jan 15, 2016
Messages
2
Hello,

this is my first post!

I have this table below, over multiple columns and rows.

Basically this list is my data and on a separate sheet I want to be able to select for example (Almonds, Banana etc) using a drop down box. I have then created, albeit not perfect, A V lookup to carry over the remaining rows (numbers)).

It would be amazing to have the code automatically update the dropdown box options if i add new food items.





TypeWeight(g) / NumberFatsCarbsProteinSugar
Milk - Almond - Unsweetened1001.20.40.5
Almonds100552022
Asparagus1000421
Avocado1001592
Bagel - Wholemeal - NewYorkBagel12.540.8114.3
Banana100025112
Beef - Sliced - ALDI10030.5270.5
Beef - Brisket - Musclefood1007021
Beef - Hache Steak - Musclefood1002321
Beef - Sliced Beef - Musclefood1008120
Biscuit - Lotus11.55.70.43
Brocolli1000732

<colgroup><col><col><col span="4"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi JTTrickett,

Welcome to the forum!

You can just use a data validation list and have the source extend past the current last line and tick the "ignore blanks" box?

But if you really want a VBA solution...

Right click on the sheet tab you want to have the drop down on, select view code - paste below

Code:
Private Sub Worksheet_Activate()

LastRow = Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]").Range("A" & Rows.Count).End(xlUp).Row

ValRange = "=[COLOR="#FF0000"]Sheet1[/COLOR]!$A$2:$A$" & LastRow

    With Sheets("[COLOR="#FF0000"]Sheet2[/COLOR]").Range("[COLOR="#0000FF"]A1[/COLOR]").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=ValRange
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With

End Sub

Change the red sheet names as appropriate, this will put the dropdown in Cell A1 of the second sheet (Change blue to required cell)

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Hi Alan,

Thank you very much for this. I am trying to teach myself all things VBA so trying to use it wherever possible.

When i try to enter the code it says it needs debugging between: .Add Type .... Forumla1 : = valrange.

However, due to my lack of knowledge ! I am not sure what needs correcting. For more information:

"sheet1" ( where the list is ) is called Food Macros and "sheet2* (destination) is called Daily Planner. I intend to have dropdowns from A3 - A42.

Many thanks!

Josh
 
Upvote 0
Hi josh,

My apologies, I left out the Apostrophes when defining the ValRange...

I've added in a loop to apply it to cells A3:A42

I'm trying to teach myself VBA too, I've found that your 2 best friends are the Macro Recorder and this forum!

Code:
Private Sub Worksheet_Activate()

LastRow = Sheets("Food Macros").Range("A" & Rows.Count).End(xlUp).Row

ValRange = "=[COLOR="#FF0000"]'[/COLOR]Food Macros[COLOR="#FF0000"]'[/COLOR]!$A$2:$A$" & LastRow

For i = 3 To 42
    With Sheets("Daily Planner").Range("A" & i).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=ValRange
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
Next

End Sub

Please feel free to ask any further questions.

Hope this helps,
Cheers,
Alan.
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

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