【发布时间】:2017-02-18 19:43:05
【问题描述】:
我有这个 vba excel 服装公式:
'=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当你没有比赛时不要做这个作业。实际上你根本不需要这个临时变量,它是对所有单元格执行的无用副本,包括那些不匹配的单元格! -
A
String永远不可能是Empty,所以Not IsEmpty(toConcatenateCellValue)永远是True。