2013-11-27 26 views
1

如何優化這些函數?他們工作,但我需要它快得多。如何優化一組分析字符串和變體類型的VBA函數

實際工作(慢):

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 

回答

2

關於IsInArray過濾將無法​​正常工作,但加入作品就好了。這假定重音符字符不是陣列(不是一個嚴重關切!):

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

對於第一種:

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

不知道我理解你tyring什麼假設你只想知道使用的行數:

Function GetNumberOfRows(list As String) As Integer 

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

End Function 

或者如果你想專門查看第二列中的行UMN:

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 
0

對於GetNumberOfRows,你傳遞一個工作表名作爲參數的函數?試試這個:

Function GetNumberOfRows(list As String) As Integer 

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

End Function 

我說「-1」如果你不想算頭(你啓動的功能第2行算起)

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 
相關問題