选择错误的范围

时间:2018-10-25 12:35:26

标签: excel vba excel-vba

我正在尝试使用多个过滤器快速计算一长串唯一值。

Rwave2生成的范围从Rwave的末尾开始,以某种方式取自原始“导出”范围的值。

s的第一个值= 44928和e = 85991,第二个值s = 1和e =2388。但是,第二个范围从原始“导出”单元格范围的89855开始,之后为2388。不是Rwave范围的1-2388。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("B4")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
           Dim wave As String
           wave = CStr(Range("B4").Value)
           Dim Rwave As Range
           Dim s, e As Long
           'Separate by Wave
           s = Search_Start(Sheets("Export").Range("A:A"), "A", wave)
           e = Search_End(Sheets("Export").Range("A:A"), "A", wave, s)
           Set Rwave = Sheets("Export").Range(Sheets("Export").Cells(s, "A"), Sheets("Export").Cells(e, "G"))
           Sheets("TestSheet1").UsedRange.ClearContents
           Rwave.Copy Sheets("TestSheet1").Range("A1")

           For i = 6 To 56
            'Separate by Zone
            Dim Rwave2 As Range
            s = Search_Start(Rwave, "B", CStr(Sheets("Sheet1").Cells(i, "B")))
            e = Search_End(Rwave, "B", CStr(Sheets("Sheet1").Cells(i, "B")), s)
            Set Rwave2 = Rwave.Range(Rwave.Cells(s, "A"), Rwave.Cells(e, "G"))
            Sheets("TestSheet2").UsedRange.ClearContents
            Rwave2.Copy Sheets("TestSheet2").Range("A1")

            'Create an array of only the unique locations
            Dim tmp, cell As String
            Dim arr() As String
            tmp = "|"
            For j = 1 To Rwave2.Rows.Count
                'Only count the locations on the right level
                If Rwave2.Cells(j, "C") = Sheets("Sheet1").Cells(i, "C") Then
                    cell = Rwave2.Cells(j, "D")
                    If (cell <> "") And (InStr(tmp, cell) = 0) Then
                        tmp = tmp & cell & "|"
                    End If
                End If
            Next j
            If Len(tmp) > 0 Then tmp = Left(tmp, Len(tmp) - 1)
            arr = Split(tmp, "|")

            Cells(i, "M") = UBound(arr) - LBound(arr)
           Next i
    End If
End Sub

Function Search_Start(r As Range, c As String, y As String) As Double
    For i = 1 To r.Rows.Count
        If InStr(r.Cells(i, c), y) <> 0 Then
                Search_Start = i
            Exit Function
        End If
    Next i
    Search_Start = 1
End Function

Function Search_End(r As Range, c As String, y As String, s As Variant) As Double
    For i = s To r.Rows.Count
        If InStr(r.Cells(i, c), y) = 0 Then
            Search_End = i - 1
            Exit Function
        End If
    Next i
    Search_End = r.Rows.Count
End Function

1 个答案:

答案 0 :(得分:0)

想想我现在正在发生什么。

以下代码将搜索C范围内的列B5:D10
因为搜索范围是从B列开始的-所以C列是第三列,当查看整个工作表时,它是D列。

类似地,您正在计算范围内的行。如果工作表单元格D7包含单词Yellow,则它将返回i=3,因为这是您范围内的第三行。

Sub Test()

    Debug.Print Search_Start(Sheet3.Range("B5:D10"), "C", "Yellow")

End Sub

Function Search_Start(r As Range, c As String, y As String) As Double
    Dim i As Long

    For i = 1 To r.Rows.Count
        Debug.Print r.Cells(i, c).Address
        If InStr(r.Cells(i, c), y) <> 0 Then
                Search_Start = i
            Exit Function
        End If
    Next i
    Search_Start = 1
End Function  

要返回正确的数字,请使用Search_Start = r.Cells(i, c).Row