将下方代码复制,并调试编译


Private Sub ListBox1_Change()

If Reload Then Exit Sub '加载ListBox1

For i = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(i) = True Then t = t & "," & ListBox1.List(i)

Next

ActiveCell = Mid(t, 2)

End Sub

Private Sub ListBox2_Change()

If Reload Then Exit Sub '加载ListBox2

For i = 0 To ListBox2.ListCount - 1

If ListBox2.Selected(i) = True Then t = t & "," & ListBox2.List(i)

Next

ActiveCell = Mid(t, 2)

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With ListBox1

'第 2 列 且 单元格大于 1,因为表头的字段不需要进行多选

If ActiveCell.Column = 2 And ActiveCell.Row > 1 Then

t = ActiveCell.Value

Reload = True '如果是根据单元格的值修改列表框,则暂时屏蔽listbox的change事件。

For i = 0 To .ListCount - 1 '根据活动单元格内容修改列表框中被选中的内容

If InStr(t, .List(i)) Then

.Selected(i) = True

Else

.Selected(i) = False

End If

Next

Reload = False

.Top = ActiveCell.Top + ActiveCell.Height '以下语句根据活动单元格位置显示列表框

.Left = ActiveCell.Left

.Width = ActiveCell.Width

.Visible = True

Else

.Visible = False

End If

End With

With ListBox2

'第 4 列 且 单元格大于 1,因为表头的字段不需要进行多选

If ActiveCell.Column = 4 And ActiveCell.Row > 1 Then

t = ActiveCell.Value

Reload = True '如果是根据单元格的值修改列表框,则暂时屏蔽listbox的change事件。

For i = 0 To .ListCount - 1 '根据活动单元格内容修改列表框中被选中的内容

If InStr(t, .List(i)) Then

.Selected(i) = True

Else

.Selected(i) = False

End If

Next

Reload = False

.Top = ActiveCell.Top + ActiveCell.Height '以下语句根据活动单元格位置显示列表框

.Left = ActiveCell.Left

.Width = ActiveCell.Width

.Visible = True

Else

.Visible = False

End If

End With

End Sub


调试 >> 编译