Results 1 to 20 of 20

Filter a List Using Different Criteria

Threaded View

  1. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,691

    Re: Hi I am Having problem in Excel Sheet

    Option Explicit
    
    Sub demo()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim a, b, c, fuels, dept, fType, x, y
    Dim lr As Long, i As Long, j As Long, n As Long, nmax As Long, nn(3) As Long, nx As Long
    Dim vDate As Long
    
    fuels = Array("Diesel", "Diesel to Petrol", "Petrol", "Filter", "Grease", "M. Oil", "Service")
    Set ws1 = Sheets("Test")
    Set ws2 = Sheets("Day total")
    Set ws3 = Sheets("Define")
    
    ReDim b(1 To 20, 1 To 29)
    ReDim c(1 To 30, 1 To 5)
    
    With ws1
        a = .[A1].CurrentRegion
    End With
    With ws3
        dept = .Range("C7:C20")
    End With
    With ws2
        vDate = .Range("C2")
       .[H6].Resize(30, 20).ClearContents
    End With
     
    For j = 0 To 2
        fType = fuels(j)                        ' Type of fuel
        nn(j) = 0
        For i = 1 To UBound(dept, 1)             ' Loop through Departments
            
                    x = Application.SumIfs(ws1.Range("N:N"), ws1.Range("C:C"), vDate, ws1.Range("I:I"), fType, ws1.Range("H:H"), dept(i, 1))
                    If x > 0 Then
                        nn(j) = nn(j) + 1: n = nn(j)
                        b(n, j * 5 + 6) = n: b(n, j * 5 + 7) = dept(i, 1): b(n, j * 5 + 8) = fType: b(n, j * 5 + 9) = x
                        
                        If j = 0 Then    ' If "Petrol" found the check for "Diesel to Petrol"
                           nn(3) = nn(3) + 1: n = nn(3)
                           b(n, j * 5 + 1) = n: b(n, j * 5 + 2) = dept(i, 1): b(n, j * 5 + 3) = fType: b(n, j * 5 + 4) = x
                           y = Application.SumIfs(ws1.Range("N:N"), ws1.Range("C:C"), vDate, ws1.Range("I:I"), fuels(1), ws1.Range("H:H"), dept(i, 1))
                           If y > 0 Then
                                nn(3) = nn(3) + 1: n = nn(3)
                                b(n, j * 5 + 1) = n: b(n, j * 5 + 2) = dept(i, 1): b(n, j * 5 + 3) = fuels(1): b(n, j * 5 + 4) = y
                           End If
                        End If
                        
                        nx = nx + 1         ' Create Summary for main fuels
                        c(nx, 1) = nx: c(nx, 2) = dept(i, 1): c(nx, 3) = fType: c(nx, 4) = x
                        
                    End If
        Next i
        nmax = Application.Max(nmax, n)
    Next j
        
    For j = 3 To 6                               ' Create remainder of Summary
        fType = fuels(j)                         ' Type of "Others"
        For i = 1 To UBound(dept, 1)             ' Loop through Departments
             x = Application.SumIfs(ws1.Range("P:P"), ws1.Range("C:C"), vDate, ws1.Range("I:I"), fType, ws1.Range("H:H"), dept(i, 1))
             If x > 0 Then
                nx = nx + 1
                c(nx, 1) = nx: c(nx, 2) = dept(i, 1): c(nx, 3) = fType: c(nx, 5) = x
             End If
         Next i
    Next j
    
    With ws2
        .[H6].Resize(nmax, 19) = b
        .[B6].Resize(nx, 5) = c
    End With
     
    End Sub

    Why not just have the summary table as there just duplication of data?
    Attached Files Attached Files
    Last edited by JohnTopley; 02-26-2025 at 05:07 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hi I am Having problem in Excel Sheet
    By anasmobikom in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 02-17-2025, 06:35 AM
  2. [SOLVED] Problem with Excel Sheet?
    By onewishtobegranted in forum Excel General
    Replies: 2
    Last Post: 08-30-2020, 11:50 PM
  3. Replies: 10
    Last Post: 12-24-2016, 04:43 PM
  4. Problem regarding excel sheet
    By intouch01 in forum Excel General
    Replies: 1
    Last Post: 12-16-2014, 02:08 AM
  5. Replies: 1
    Last Post: 07-15-2013, 05:25 AM
  6. Facing problem in excel while copying data from one sheet to another sheet
    By krasathi in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-03-2012, 04:40 AM
  7. Replies: 0
    Last Post: 04-02-2005, 09:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1