0
我有這樣的VBA練成costum式:Excel的VBA costum式太慢
'=ConcatenateRangeIfs(A1;Sheet2!C:C;B1;Sheet2!D:D;Sheet2!G:G;". ")
Function ConcatenateRangeIfs(_
ByVal match_val1 As String, _
ByVal match_range1 As Range, _
ByVal match_val2 As String, _
ByVal match_range2 As Range, _
ByVal concatenate_range As Range, _
Optional ByVal separator As String _
) As String
'disable uncessary processing to improve performance
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Dim concatedString As String
Dim toConcatenateCellValue As String
Dim toConcatenateCellRow As Long
For Each toConcatenateCell In concatenate_range.SpecialCells(xlConstants, 23)
toConcatenateCellValue = toConcatenateCell.Value
If Not IsEmpty(toConcatenateCellValue) Then
toConcatenateCellRow = toConcatenateCell.Row
If match_val1 = match_range1.Cells(toConcatenateCellRow, 1).Value Then
If match_val2 = match_range2.Cells(toConcatenateCellRow, 1).Value Then
concatedString = concatedString & (separator & toConcatenateCellValue)
End If
End If
End If
Next toConcatenateCell
If Len(concatedString) <> 0 Then
concatedString = Right$(concatedString, (Len(concatedString) - Len(separator)))
End If
'enable disabled processing
ConcatenateRangeIfs = concatedString
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End Function
不明白爲什麼,但每次更改公式中使用的任何值時,它會花費太長的時間和凍結excel。 我試過禁用不必要的excel東西,並使用本地可訪問性來訪問對象屬性,但並沒有多大改變...
任何消音來提高性能?
第一件事,我可以發現:'toConcatenateCellValue = toConcatenateCell.Value'不這樣做分配,當你沒有一個匹配。實際上,你根本不需要這個臨時變量,它是在所有單元格上執行的無用副本,包括那些不匹配的單元格! –
一個'String'永遠不能是'Empty',所以'不是IsEmpty(toConcatenateCellValue)'總是會成爲'True'。 – YowE3K