用了VB 实现下拉多选,目前只能在A2及以下的单元格里出现选项栏,如何改成别的单元格比如:J5到J20
vb代码如下:
Private Sub ListBox1_Change()
If Reload Then Exit Sub '见下方说明
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 Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
If ActiveCell.Column = 1 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
15 个解决方案
#1
If ActiveCell.Column = 1 And ActiveCell.Row > 1 Then 你给限制了啊
#2
我一点代码都不懂,代码从网上找的,如何修改啊?
#3
你的代码中:
If ActiveCell.Column = 1 And ActiveCell.Row > 1 Then
........
这个就表示: 如果活动单元格的列号=1(
A列)并且行号>1,就执行 …………
同理,如果你想改成“J5到J20”,那么,条件就得改成:
If ActiveCell.Column = 10 And ActiveCell.Row >=5 And ActiveCell.Row <=20 Then
..........
Private Reload As Boolean
Private t As String
Private i As Long
Private Sub ListBox1_Change()
If Reload Then Exit Sub
t = ""
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 Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
If ActiveCell.Column = 10 And ActiveCell.Row >= 5 And ActiveCell.Row <= 20 Then
t = ActiveCell.Value
Reload = True
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
Private Reload As Boolean
Private t As String
Private i As Long
Private Sub ListBox1_Change()
If Reload Then Exit Sub
t = ""
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 Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
If ActiveCell.Column = 10 And ActiveCell.Row >= 5 And ActiveCell.Row <= 20 Then
t = ActiveCell.Value
Reload = True
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
Private Reload As Boolean
Private t As String
Private i As Long
Private Sub ListBox1_Change()
If Reload Then Exit Sub
t = ""
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 Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
If ActiveCell.Column = 10 And ActiveCell.Row >= 5 And ActiveCell.Row <= 20 Then
t = ActiveCell.Value
Reload = True
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
Private Reload As Boolean
Private t As String
Private i As Long
Private Sub ListBox1_Change()
If Reload Then Exit Sub
t = ""
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 Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
If ActiveCell.Column = 10 And ActiveCell.Row >= 5 And ActiveCell.Row <= 20 Then
t = ActiveCell.Value
Reload = True
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