ClosedXML Excel Template

wgeohagan

New Member
Joined
Apr 28, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a simple report using a ClosedXML Excel Template. I have a .net application receiving data from SQL Server and then using that data to create the report in excel based on the template. I cannot figure out how to group the WorkNumbers so their Types are together. I have given example data and how I would like the report to look. Any help on grouping would be appreciated.

Thank You
 

Attachments

  • Data.PNG
    Data.PNG
    5.9 KB · Views: 16
  • Report.PNG
    Report.PNG
    8.8 KB · Views: 17

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Pivot table should help you achieve the desired results.
 
Upvote 0
Book2
ABCDEFGH
1WorknumberTypeNumhours
2214212Direct30214212TypeNumHours
3214212Indirect8Direct30
4214213Direct45Indirect8
5214218Direct32
6214218Indirect15214213TypeNumHours
7214218Overtime12Direct45
8214213Indirect8Indirect8
9
10214218TypeNumHours
11Direct32
12Indirect15
13Overtime12
14
Sheet2


VBA Code:
Sub test()
    Dim dict As New Dictionary
    
    
    Range("a1:c" & Cells(Rows.Count, "a").End(xlUp).Row).Copy [e1]
    
     Range("e2:g8").Sort key1:=Range("e2"), order1:=xlAscending, key2:=Range("e2"), order2:=xlAscending
     
    a = Range("e2:g" & Cells(Rows.Count, "A").End(xlUp).Row)
    

   Range("e1:g" & Cells(Rows.Count, "g").End(xlUp).Row).Clear

    ReDim b(1 To 100, 1 To 7)
    
    For i = 1 To UBound(a, 1)
        If Not dict.Exists(a(i, 1)) Then
            R = R + IIf(R > 1, 2, 1)
            dict.Add a(i, 1), R
            b(R, 1) = a(i, 1)
            b(R, 2) = "Type"
            b(R, 3) = "NumHours"
        End If
        R = R + 1
        b(R, 2) = a(i, 2)
        b(R, 3) = a(i, 3)
    Next i

Range("e2").Resize(R, 3).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,319
Messages
6,171,427
Members
452,402
Latest member
siduslevis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top