更改e = sh.Range(“C”& Rows.Count)的行选择.End(xlUp).Row

时间:2017-03-11 16:59:05

标签: excel vba excel-vba

我有这段excel vba代码,有助于显示从C16列到最后一行excel的组合框。但是,我希望它只显示在行C16 - C20和C25 - 最后一行。如何使用现有代码实现此目的?我认为我所指的代码行可以改变,但我不知道如何使用它。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 And Target.Row > 15 And Target.Column = 3 Then
    Dim e As Long
    Dim sh As Worksheet
    Set sh = Sheets("Setting")
    If Not IsArray(priArray) Then


        e = sh.Range("C" & Rows.Count).End(xlUp).Row '<<<<<<<<

        priArray = sh.Range("C5:C" & e).Value2
    End If
    Call ShowComboBox
Else
    Call HideComboBox
End If
End Sub

1 个答案:

答案 0 :(得分:1)

您不能从不连续的范围设置数组的元素(也称为),即使该范围已经联合。你将不得不逐砖地构建它。

Dim priArray As Variant
Dim c As Long, e As Long

With Worksheets("Setting")
    ReDim priArray(0)
    For e = 16 To .Range("C" & .Rows.Count).End(xlUp).Row
        Select Case e
            Case 16 To 20, Is >= 25
                priArray(UBound(priArray)) = .Range("C" & e).Value2
                ReDim Preserve priArray(UBound(priArray) + 1)
            Case Else
                'do nothing
        End Select
    Next e
    ReDim Preserve priArray(UBound(priArray) - 1)
End With

'do something with the loaded priArray here