Forum Discussion
Drop down menu linked to an Excel file
Hi! I would like to populate Word drop down menu to data stored in Excel spreadsheets. I found a post (https://learn.microsoft.com/en-us/answers/questions/5448059/drop-down-menu-linked-to-a-file) in https://learn.microsoft.com showing how to get it done, but I'm new to VBA and need a more detailed explanation. The last line in that post reads, "For a step-by-step guide, you can check out this tutorial on linking Excel drop-down lists to Word.", but no link to that step-by-step guide is provided.
Please, do you know where that guide, or any other helpful manual, is available?
Thanks!
3 Replies
- RICARDO JULIO RODRIGUEZ FERNANDEZCopper Contributor
Thanks! Both answers open a new universe to me! I started trying the proposal of Kidd_Ip reply that seems to me more straightforward, but I'm stuck with an error. Please see below for my elementary data source, the error message, and the debug highlight of the code.
Could you help me understand and work out this issue? Thanks!
See the following pages of Greg Maxey's website :
http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm
http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm
http://gregmaxey.mvps.org/Customize_Ribbon.htm
See the "Cascading Listboxes" section of the following page of Greg Maxey's website:
http://gregmaxey.mvps.org/word_tip_pages/populate_userForm_listbox_or_combobox.html
Below may be worth trying:
- A Word document with a Combo Box Content Control
- An Excel file with your list of options (e.g., in Column A)
- Access to the VBA Editor in Word (press Alt + F11)
Sub PopulateDropdownFromExcel() Dim exlApp As Object Dim xlWrkBok As Object Dim sheetName As String Dim wkbkName As String Dim LRow As Long Dim i As Long ' Excel file path and sheet name wkbkName = "C:\Path\To\Your\File.xlsx" sheetName = "Sheet1" ' Start Excel Set exlApp = CreateObject("Excel.Application") Set xlWrkBok = exlApp.Workbooks.Open(wkbkName, ReadOnly:=True) With xlWrkBok.Worksheets(sheetName) LRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp Selection.Range.ContentControls(1).DropdownListEntries.Clear For i = 1 To LRow Selection.Range.ContentControls(1).DropdownListEntries.Add Text:=Trim(.Cells(i, 1).Value) Next i End With xlWrkBok.Close SaveChanges:=False exlApp.Quit End Sub
- Open your Word document.
- Insert a Combo Box Content Control from the Developer tab.
- Press Alt + F11 to open the VBA editor.
- Paste the code into a new module.
- Run the macro (F5) while your cursor is inside the Combo Box.