我试图根据Jeeped先生更改我的vba代码,以便它只显示excel中7-10和15到最后一行的选定范围内的下拉框。但是,我没有工作,我仍然可以在第11行选择下拉框。
这是一段代码,但是,我注意到Jeeped先生的这个版本,速度非常慢。
我也在这里有测试文件,请看一下:download
我的原始代码:显示从第7行到最后一行的下拉框。
Private priArray
Private priIsFocus As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 And Target.Row > 6 And Target.Column = 1 Then
Dim e As Long
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
If Not IsArray(priArray) Then
e = sh.Range("C" & Rows.Count).End(xlUp).Row
priArray = sh.Range("C5:C" & e).Value2
End If
Call HienComboBox
Else
Call AnComboBox
End If
End Sub
根据Jeeped先生的建议,以下是修改后的代码:
Private priArray
Private priIsFocus As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 And Target.Row > 6 And Target.Column = 1 Then
Dim e As Long
With Worksheets("Sheet1")
ReDim priArray(0)
For e = 7 To .Range("A" & .Rows.Count).End(xlUp).Row
Select Case e
Case 7 To 10, Is >= 15
priArray(UBound(priArray)) = .Range("A" & e).Value2
ReDim Preserve priArray(UBound(priArray) + 1)
Case Else
'do nothing
End Select
Next e
ReDim Preserve priArray(UBound(priArray) - 1)
End With
Call HienComboBox
Else
Call AnComboBox
End If
End Sub