如何优化解析String和Variant类型的一组VBA函数

时间:2013-11-27 20:18:14

标签: vba excel-vba excel

如何优化这些功能?他们工作但我需要它更快。

实际工作(慢):

  Function IsInArray(value As String, arr As Variant) As Boolean
        Dim length As Integer
        Dim found As Boolean

        length = UBound(arr)
        found = False
        i = 0
        While Not found And i < length
            If arr(i) = value Then
                found = True
            End If

            i = i + 1
        Wend
        If found Then
            IsInArray = True
        Else
            IsInArray = False
        End If
    End Function

这会获得特定列表中的行数:

Function GetNumberOfRows(list As String) As Integer

    Dim numRows As Integer
    Dim row As Integer
    Dim column As Integer

    row = 2
    column = 2
    numRows = 0

    While (Worksheets(list).Cells(row, column).value <> "")
        numRows = numRows + 1
        row = row + 1
    Wend

    GetNumberOfRows = numRows

End Function

这会替换范围

中的值
Sub ReplaceValue(oldValue As String, newValue As String, list As String)

    Dim numRows, numColumns As Integer
    Dim row, column As Integer

    numRows = GetNumberOfRows(list)
    numColumns = 9

    row = 2
    While row <= numRows + 1

        column = 3
        While column <= numColumns + 3

            If Worksheets(list).Cells(row, column).value = oldValue Then
                Worksheets(list).Cells(row, column).value = newValue
            End If

            column = column + 1
        Wend

        row = row + 1
    Wend
End Sub

P.S。

这很快但不是规格,好像我正在搜索“aa”,如果“aab”在数组中,它将显示为TRUE。然而,将它作为“快速”的例子包括在内可能是有益的。

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

4 个答案:

答案 0 :(得分:2)

关于 IsInArray 过滤器不起作用,但加入工作正常。这假设重音符号不在数组中(不是一个严重的问题!):

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    jn = "`" & Join(arr, "`") & "`"
    IsInArray = InStr(1, jn, "`" & stringToBeFound & "`") > 0
End Function

答案 1 :(得分:1)

第一个:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function

我不知道你是否理解你在使用第二个时所做的事情,假设你只是想知道所使用的行数:

Function GetNumberOfRows(list As String) As Integer

    GetNumberOfRows = Worksheets(list).UsedRange.Rows.Count -1       

End Function

或者,如果您特别想要第二列中的行:

Function GetNumberOfRows(list As String) As Integer

    GetNumberOfRows = Worksheets(list).Cells(rows.count,"B").End(xlUp).Row     

End Function

第三个:

Sub ReplaceValue(oldValue As String, newValue As String, list As String)

Worksheets(list).UsedRange.Replace What:=oldValue, Replacement:=newValue, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

End Sub

答案 2 :(得分:0)

对于函数 GetNumberOfRows ,您是否将工作表名称作为参数传递?试试这个:

Function GetNumberOfRows(list As String) As Integer

    GetNumberOfRows = Worksheets(list).UsedRange.Rows.Count - 1

End Function

如果您不想计算标题,我添加了'-1'(您的功能在第2行开始计算)

答案 3 :(得分:0)

检查是否在数组中:

Function IsInArray(value As String, arr As Variant) As Boolean

    If Not IsArray(arr) Then
        'the argument arr is not an array, return false
        IsInArray = False
        Exit Function
    End If

    IsInArray = (InStr(1, "||" & Join(arr, "||") & "||", "||" & value & "||", vbTextCompare) > 0)

End Function

获取行数:

Function GetNumberOfRows(list As String) As Integer

    With Sheets(list)
        GetNumberOfRows = WorksheetFunction.Max(0, .Rows.Count - WorksheetFunction.CountBlank(.Columns("B")) - 1)
    End With

End Function

替换值:

Sub ReplaceValue(oldValue As String, newValue As String, list As String)

    With Intersect(Sheets(list).UsedRange, Sheets(list).Range("C:L"))
        .Replace oldValue, newValue, xlWhole
    End With

End Sub