要在一個sheet表中,設定兩個下拉多選選單,如果分別編代碼的話,第二個下拉選單無法隱藏,想請教各位大大如何可以把兩段代碼合并成一段運行。
Private Sub ListBox1_Change()
If Reload Then Exit Sub '見下方說明
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then t = t & "" & Chr(10) & ListBox1.List(i)
Next
ActiveCell = Mid(t, 2)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Column = 7 And ActiveCell.Row > 1 Then
t = ActiveCell.Value
Reload = True '如果是根據單元格的值修改串列框,則暫時屏蔽listbox的change事件。
With ListBox1
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
End With
Else
ListBox1.Visible = False
End If
End Sub
Private Sub ListBox2_Change()
If Reload Then Exit Sub '見下方說明
For i = 0 To ListBox1.ListCount - 2
If ListBox1.Selected(i) = True Then t = t & "" & Chr(10) & ListBox1.List(i)
Next
ActiveCell = Mid(t, 2)
End Sub
Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
If ActiveCell.Column = 9 And ActiveCell.Row > 1 Then
t = ActiveCell.Value
Reload = True '如果是根據單元格的值修改串列框,則暫時屏蔽listbox的change事件。
With ListBox1
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
End With
Else
ListBox1.Visible = False
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/21784.html
標籤:VBA
上一篇:VBA通過dos命令操作SVN,對已經提交的代碼加注釋。
下一篇:請問關于變數生命周期的問題
