Advertisement
YasserKhalil2019

T4209_Using SUMIFS In VBA From Multiple Sheets

Oct 26th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. https://excel-egy.com/forum/t4209
  2. ---------------------------------
  3.  
  4. Sub Using_SUMIFS_In_VBA_From_Multiple_Sheets()
  5. Dim e, ws As Worksheet, wsSource As Worksheet, wf As Object, iCol As Integer, iStart As Integer, iEnd As Integer, i As Integer, rngB As Range, rngC As Range, rngX As Range
  6.  
  7. Application.ScreenUpdating = False
  8. Set ws = ThisWorkbook.Worksheets("اجمالى المبيعات")
  9. Set wf = Application.WorksheetFunction
  10. ws.Range("B5:F" & Rows.Count).ClearContents
  11.  
  12. For Each e In Array("XLSB", "XLSM", "XLS")
  13. If e = "XLSB" Then
  14. Set wsSource = ThisWorkbook.Worksheets("XLSB_SEARCH"): iCol = 16: iStart = 5: iEnd = 10
  15. ElseIf e = "XLSM" Then
  16. Set wsSource = ThisWorkbook.Worksheets("XLSM_SEARCH"): iCol = 17: iStart = 11: iEnd = 12
  17. ElseIf e = "XLS" Then
  18. Set wsSource = ThisWorkbook.Worksheets("XLS_SEARCH"): iCol = 19: iStart = 13: iEnd = 15
  19. End If
  20.  
  21. Set rngB = wsSource.Range("B:B")
  22. Set rngC = wsSource.Range("C:C")
  23. Set rngX = wsSource.Columns(iCol)
  24.  
  25. With ws
  26. For i = iStart To iEnd
  27. ws.Cells(i, 2) = wf.SumIfs(rngB, rngX, ws.Cells(i, 1))
  28. ws.Cells(i, 3) = wf.SumIfs(rngC, rngX, ws.Cells(i, 1))
  29. Next i
  30. End With
  31. Next e
  32.  
  33. ws.Cells(16, 2) = wf.Sum(ws.Range("B5", "B15"))
  34. ws.Cells(16, 3) = wf.Sum(ws.Range("C5", "C15"))
  35. For i = 5 To 16
  36. ws.Cells(i, 6) = wf.Sum((ws.Cells(i, 3)) - (ws.Cells(i, 4)))
  37. Next i
  38. Application.ScreenUpdating = True
  39. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement