Microsoft Word - trouble with arrays - limit one list based on choice in another

RustyNC

New Member
Joined
Jan 10, 2011
Messages
6
I have a Word document, written by someone else in my company, with a macro. The purpose is to dynamically populate one list of choices based on a choice in a previous list. As you can see below, the macro creates some static arrays and then checks the value from the first list 'process' and fills 'subprocess' with the associated list.

An abbreviated sample of the code is at the bottom.

This code is not very dynamic and cumbersome to allow for new values. The person that adds and updates the arrays doesn't understand the code enough to change it properly. A new choice was added to 'process' and then all the 'subprocess' choices no longer matched. That is why I was called to help. I figured out how to get it working, but realized how painful it was to change.

The number of arrays is around 20 in the full document. The array with the most elements has UBound = 27. When a new choice was added to the first dropdown list it created the need to change most of the hard-coded values used in the Select Case.

I thought I would be able to use a dynamic string to supply the correct array to the list, but was unable to figure out how to do so.

Should the code remain as it is or be changed to be more dynamic, and hopefully easier to manipulate when new lists are added? Does anyone know a better way?

Code:
Sub FieldExit()
    Dim P9221(20) As String
    Dim P9385(20) As String
    Dim var
 
    P9221(0) = "0010 IT budget"
    P9221(1) = "0050 IT management"
 
    P9385(0) = "0100 Management"
    P9385(1) = "0150 Research"
    P9385(2) = "0200 Markets"
    P9385(3) = "0800 Safety"
    P9385(4) = "0850 Projects"
 
    ' use the value of the dropdown to case select condition
    ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Clear
 
    Select Case ActiveDocument.FormFields("process").DropDown.Value
            Case 2
            For var = 1 To 2
                ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Add Name:=P9221(var - 1)
            Next
            Case 3
            For var = 1 To 5
                ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Add Name:=P9385(var - 1)
            Next
    End Select
 
End Sub

This code is for Microsoft Word 2010 using Windows 7 OS.

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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