|
楼主 |
发表于 2018-9-12 15:56
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Private Sub ListBox1_click()
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then s = .List(i)
Next
End With
er = Cells(65536, "R").End(xlUp).Row
r = Range("Q:Q").Find(s).Row
Me.ListBox3.Clear
Me.ListBox4.Clear
For i = r To er
If Cells(i, "Q") = s Or Cells(i, "Q") = "" Then
If Cells(i, "R") <> "" Then Me.ListBox3.AddItem Cells(i, "R")
If Cells(i, "S") <> "" Then Me.ListBox4.AddItem Cells(i, "S")
Else
Exit For
End If
Next
For i = 1 To 4
Me.OLEObjects("ListBox" & i).Visible = True
Next
End Sub
Private Sub ListBox2_Click()
With Me.ListBox2
For i = 0 To .ListCount - 1
If .Selected(i) Then s = .List(i)
Next
End With
If Len(s) > 0 Then
If Len(Cells(ActiveCell.Row, 1)) > 0 Then
xyes = MsgBox("目标单元格数据存在,覆盖?", vbYesNo)
If xyes = vbYes Then Cells(ActiveCell.Row, 1) = s
Else
Cells(ActiveCell.Row, 1) = s
End If
End If
End Sub
Private Sub ListBox3_Click()
With Me.ListBox3
For i = 0 To .ListCount - 1
If .Selected(i) Then s = .List(i)
Next
End With
If Len(s) > 0 Then
If Len(Cells(ActiveCell.Row, 2)) > 0 Then
xyes = MsgBox("目标单元格数据存在,覆盖?", vbYesNo)
If xyes = vbYes Then Cells(ActiveCell.Row, 2) = s
Else
Cells(ActiveCell.Row, 2) = s
End If
End If
End Sub
Private Sub ListBox4_Click()
With Me.ListBox4
For i = 0 To .ListCount - 1
If .Selected(i) Then s = .List(i)
Next
End With
If Len(s) > 0 Then
If Len(Cells(ActiveCell.Row, 3)) > 0 Then
xyes = MsgBox("目标单元格数据存在,覆盖?", vbYesNo)
If xyes = vbYes Then Cells(ActiveCell.Row, 3) = s
Else
Cells(ActiveCell.Row, 3) = s
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Row >= 0 And Target.Column = 5 Then
Me.ListBox1.Clear
er = Cells(65536, "Q").End(xlUp).Row
For i = 3 To er
If Cells(i, "Q") <> "" Then Me.ListBox1.AddItem Cells(i, "Q")
Next
Me.ListBox2.Clear
er = Cells(65536, "P").End(xlUp).Row
For i = 3 To er
If Cells(i, "P") <> "" Then Me.ListBox2.AddItem Cells(i, "P")
Next
t = Target.Offset(1, 1).Top + 1
Me.ListBox1.Left = Target.Offset(1, 1).Left
Me.ListBox1.Visible = True
Me.ListBox2.Visible = True
For i = 1 To 4
With Me.OLEObjects("ListBox" & i)
.Top = t
If i > 1 Then .Left = Me.OLEObjects("ListBox" & i - 1).Left + Me.OLEObjects("ListBox" & i - 1).Width + 1
End With
Next
Else
For i = 1 To 4
Me.OLEObjects("ListBox" & i).Visible = False
Next
End If
End Sub
代码是上面这些。 |
|