I have this vba excel costum formula:
'=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
The sheet1 example where the formula is in column D:D cells:
Don't understand why but it takes too long and freezes excel every time I change any of the values used in the formula. I've tried disabling unecessary excel stuff, and use local veriables to access objects properties but didn't change much...
Any sugestion to improve performance?
toConcatenateCellValue = toConcatenateCell.Value
dont do this assignment when you don't have a match. You don't need this temporary variable at all in fact, it's a useless copy that is performed on all the cells including those that don't match! – A.S.HString
can never beEmpty
, soNot IsEmpty(toConcatenateCellValue)
is always going to beTrue
. – YowE3K