Guest User

Untitled

a guest
Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. SUMIF3D(ByVal sum_range As Range, ByVal list_Sheets As String, ByVal criteriaRange As Range, ByVal criteria As Variant, Optional ByVal isNumeric As Boolean = False)
  2.  
  3. =SUMIF3D(D1:D5,"sheet1,sheet2",H1:H5,I1)
  4.  
  5. Sheet1!D1:D5 = 1-5
  6. Sheet2!D1:D5 = 10-50
  7. Sheet1!H1:H5 = 1,5,10,15,20
  8.  
  9. Option Explicit
  10.  
  11. Public Function SUMIF3D(ByVal sum_range As Range, ByVal list_Sheets As String, ByVal criteriaRange As Range, ByVal criteria As Variant, Optional ByVal isNumeric As Boolean = False) As Long
  12. Const OPERATORS As String = ">,<,<>,="
  13. Dim isPossible As Boolean
  14. Dim toSum() As Boolean
  15. Dim i As Long
  16. Dim j As Long
  17.  
  18. Dim sumRangeCells() As Long
  19. ReDim sumRangeCells(1 To sum_range.Count, 1 To 2)
  20. Dim cell As Range
  21. i = 1
  22. For Each cell In sum_range
  23. sumRangeCells(i, 1) = cell.Row
  24. sumRangeCells(i, 2) = cell.Column
  25. i = i + 1
  26. Next
  27.  
  28. Dim numberOfCells As Long
  29. Dim sheetsArray As Variant
  30. sheetsArray = Split(list_Sheets, ",")
  31. Dim sumRangeArray As Variant
  32. numberOfCells = (UBound(sheetsArray) + 1) * sum_range.Count
  33. ReDim sumRangeArray(1 To numberOfCells)
  34.  
  35. Dim k As Long
  36. k = 1
  37. For i = LBound(sheetsArray) To UBound(sheetsArray)
  38. For j = 1 To sum_range.Count
  39. sumRangeArray(k) = Sheets(sheetsArray(i)).Cells(sumRangeCells(j, 1), sumRangeCells(j, 2))
  40. k = k + 1
  41. Next
  42. Next
  43.  
  44. Dim critRangeArray As Variant
  45. critRangeArray = criteriaRange.Value2
  46. Dim criteriaArray As Variant
  47. criteriaArray = criteria.Value2
  48.  
  49. ReDim toSum(1 To UBound(critRangeArray, 1))
  50. If Not IsArray(criteriaArray) Then
  51. If IsEmpty(criteriaArray) Then
  52. isPossible = False
  53. Else: isPossible = True
  54. End If
  55. Else
  56. If Not UBound(criteriaArray, 1) = UBound(critRangeArray, 1) Then
  57. isPossible = False
  58. Else: isPossible = True
  59. End If
  60. End If
  61.  
  62. If Not isPossible Then Exit Function
  63.  
  64. Select Case isNumeric
  65. Case 1
  66. If IsArray(criteriaArray) Then
  67. For i = 1 To UBound(critRangeArray)
  68. If InStr(1, OPERATORS, Left$(criteriaArray(i, 1), 1)) > 0 Then
  69. toSum(i) = Application.Evaluate(critRangeArray(i, 1) & criteriaArray(i, 1))
  70. Else: toSum(i) = critRangeArray(i, 1) = criteriaArray(i, 1)
  71. End If
  72. Next
  73. Else
  74. For i = 1 To UBound(critRangeArray)
  75. If InStr(1, OPERATORS, Left$(criteriaArray, 1)) > 0 Then
  76. toSum(i) = Application.Evaluate(critRangeArray & criteriaArray)
  77. Else: toSum(i) = critRangeArray(i, 1) = criteriaArray
  78. End If
  79. Next
  80. End If
  81. Case 0
  82. If IsArray(criteriaArray) Then
  83. For i = 1 To UBound(critRangeArray)
  84. toSum(i) = critRangeArray(i, 1) = criteriaArray(i, 1)
  85. Next
  86. Else
  87. For i = 1 To UBound(critRangeArray)
  88. toSum(i) = critRangeArray(i, 1) = criteriaArray
  89. Next
  90. End If
  91. End Select
  92.  
  93. For j = LBound(sheetsArray) To UBound(sheetsArray)
  94. For i = 1 To UBound(toSum)
  95. If toSum(i) Then SUMIF3D = SUMIF3D + sumRangeArray(i + j * UBound(toSum))
  96. Next
  97. Next
  98. End Function
Add Comment
Please, Sign In to add comment