Sorry, I tested the code while my ComboBox1 had a "real" default value. If
you have set a default value as I suggested (like "Please select a value"),
the code will point to a non-existing column 0 in Table 2 when the UserForm
initializes - and an error occurs. Below, you will find an adjusted version
of the code. The only difference is that an If - End If constructions has
been added, testing whether n (the column number) is larger than 0. Hope it
works:
Private Sub ComboBox1_Change()
'Insert values in ComboBox2 depending on the value selected in ComboBox1
'Table 2 column number to use is list item no. + 1 (because list count
starts at 0)
'Add values from row 2 and downwards in Table 2
Dim oCell As Cell
Dim n As Long
Dim strName As String
'Find column to look in - value in Combobox1 determines which column to
use
n = ComboBox1.ListIndex + 1
With ComboBox2
'Delete existing items
.Clear
'n = 0 if a ComboBox1 shows a default value that is not in the list
If n > 0 Then
For Each oCell In ActiveDocument.Tables(2).Columns(n).Cells
'Skip first row
If oCell.RowIndex > 1 Then
'Get text from cell - excl. cell marker
strName = Left(oCell.Range.Text, Len(oCell.Range.Text) -
2)
'Only add item if the result is not "" (empty cell)
If strName <> "" Then
.AddItem strName
End If
End If
Next oCell
'Set default value (could also be e.g. .List(0) - i.e. first name)
.Value = "Select a name"
End If
End With
End Sub
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word