VBA to generate report

dsubash

New Member
Joined
Nov 22, 2024
Messages
32
Office Version
  1. 2019
  2. Prefer Not To Say
Platform
  1. Windows
Hi Experts,

Need some VBA Code for my reports. Given below the screenshot of my database and reports page. I have used normal excel formulas to derive at this report. But it takes too much time to generate and sometimes excel hangs up.

Sheet1 (Data)
Sample Test Details TDM.xlsx
ABCDEFGHIJKLMNOPQ
1No.Sample No.Sample IDTestResultRegister DateNameAgeGenderCase No.Bed No.DoctorDepartmentDiagnosisRemarkCheckerTest Date
211062339000106233900 UREA2901/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:13
321062339000106233900 SGOT19.601/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:16
431062339000106233900 GGT24.601/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:17
541062339000106233900 TP5.1501/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:17
651062339000106233900 CHOL16901/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:15
761062339000106233900 BILL-D0.3201/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:16
871062339000106233900 ALP7001/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:16
981062339000106233900 BILL-T101/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:15
1091062339000106233900 CAL A11.301/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:14
11101062339000106233900 TGL13701/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:15
12111062339000106233900 SGPT2501/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:16
13121062339000106233900 CREATIN1.0301/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:18
14131062339000106233900 ALB4.0301/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:17
15141062339000106233900 UA5.1801/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:14
16151062339000106233900 GLUC11201/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:14
17161062341000106234100 PHOS4.1201/01/2025 08:57Maharaja0YearsMale01/01/2025 09:21
18171062341000106234100 UA6.301/01/2025 08:57Maharaja0YearsMale01/01/2025 09:20
19181062341000106234100 TGL15101/01/2025 08:57Maharaja0YearsMale01/01/2025 09:21
20191062341000106234100 UREA5401/01/2025 08:57Maharaja0YearsMale01/01/2025 09:19
21201062341000106234100 CHOL19101/01/2025 08:57Maharaja0YearsMale01/01/2025 09:20
22211062341000106234100 GLUC11601/01/2025 08:57Maharaja0YearsMale01/01/2025 09:19
23221062341000106234100 CAL A10.401/01/2025 08:57Maharaja0YearsMale01/01/2025 09:20
24231062341000106234100 CREATIN2.2501/01/2025 08:57Maharaja0YearsMale01/01/2025 09:21
25241062340000106234000 CREATIN0.8601/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:19
26251062340000106234000 CHOL12001/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:18
27261062340000106234000 TGL19201/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:19
28271062340000106234000 GLUC40201/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:18
29281062342000106234200 GLUC9701/01/2025 09:59AnandKumarV0YearsMale01/01/2025 10:13
30295005736000500573600 GLUC15001/01/2025 10:19John0YearsMale01/01/2025 10:34
31305005735000500573500 GLUC9401/01/2025 10:19Pooranima0YearsFemale01/01/2025 10:33
32315005735010106234400 GLUC10101/01/2025 10:29logapriyaMale01/01/2025 10:49
33325005735020106234400 UA3.601/01/2025 10:36logapiyaMale01/01/2025 10:50
34335005735020106234400 SGPT2301/01/2025 10:36logapiyaMale01/01/2025 10:52
35345005735020106234400 ALB3.9701/01/2025 10:36logapiyaMale01/01/2025 10:53
36355005735020106234400 BILL-T0.2801/01/2025 10:36logapiyaMale01/01/2025 10:51
37365005735020106234400 TP5.2901/01/2025 10:36logapiyaMale01/01/2025 10:53
38375005735020106234400 TGL23101/01/2025 10:36logapiyaMale01/01/2025 10:51
Data


Sheet2(Report)
Sample Test Details TDM.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Sl. No.Sample IDRegister DateNameGenderTest DateUREASGOTGGTTPCHOLBILL-DALPBILL-TCAL ATGLSGPTCREATINALBUAGLUCPHOSACEIRONAMYLASEMICROCKNACMAGLDHCKMBZINC-CO
210106233900 01/01/2025 08:57NithishKumarMale01/01/2025 09:13UREASGOTGGTTPCHOLBILL-DALPBILL-TCAL ATGLSGPTCREATINALBUAGLUC          
320106234100 01/01/2025 08:57MaharajaMale01/01/2025 09:21UREA   CHOL   CAL ATGL CREATIN UAGLUCPHOS         
430106234000 01/01/2025 08:58VetrivelSMale01/01/2025 09:19    CHOL    TGL CREATIN  GLUC          
540106234200 01/01/2025 09:59AnandKumarVMale01/01/2025 10:13              GLUC          
650500573600 01/01/2025 10:19JohnMale01/01/2025 10:34              GLUC          
760500573500 01/01/2025 10:19PooranimaFemale01/01/2025 10:33              GLUC          
870106234400 01/01/2025 10:29logapriyaMale01/01/2025 10:49UREASGOT TPCHOLBILL-DALPBILL-TCAL ATGLSGPTCREATINALBUAGLUC          
980106234104 01/01/2025 11:27MaharajaMale01/01/2025 11:41              GLUC          
1090106234004 01/01/2025 11:27VetrivelSMale01/01/2025 11:40              GLUC          
11100500573604 01/01/2025 12:00johnMale01/01/2025 12:15              GLUC          
12110500573504 01/01/2025 12:01pooranimaMale01/01/2025 12:15              GLUC          
13120106234204 01/01/2025 12:02anandkumarMale01/01/2025 12:15              GLUC          
14130106235600 01/01/2025 12:41AbdulAlphafMale01/01/2025 12:54UREASGOTGGTTP BILL-DALPBILL-T  SGPTCREATINALB            
Report
Cell Formulas
RangeFormula
A2:A14A2=IF(LEN(B2)>0,SUM(A1)+1,"")
B2:B3B2=IFERROR(INDEX(Data!$C$2:$C$10000, MATCH(0, COUNTIF($B$1:B1, Data!$C$2:$C$10000), 0)), "")
C2:C14C2=IFERROR(VLOOKUP(B2, Data!$C:$Q, 4, 0),"")
D2:D14D2=IFERROR(VLOOKUP(B2,Data!$C$2:$Q$10000,5,0),"")
E2:E14E2=IFERROR(VLOOKUP(B2,Data!$C$2:$Q$10000,7,0),"")
F2:F14F2=IFERROR(VLOOKUP(B2, Data!$C$2:$Q$10000, 15, 0), "")
G2:G14G2=IF(COUNTIFS(Data!$C:$C, $B2, Data!$D:$D, G$1) > 0, G$1, "")
H2:AE14H2=IF(COUNTIFS(Data!$C$2:$C$10000, $B2, Data!$D$2:$D$10000, H$1) > 0, H$1, "")
B4:B14B4=IFERROR(INDEX(Data!C4:C962, MATCH(0, COUNTIF($B$1:B3, Data!C4:C962), 0)), "")
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
_FilterDatabase=Report!$A$1:$AE$1128A2


Help with a VBA Code to generate report would be of great help to me. My database runs to numerous rows and excel formula takes too much time to process the data.

Thanks in advance
Subash D
 
I believe that some versions of excel are smart enough to workout the last row, otherwise it gets a bit messy
You can create a named range which shrinks and grows accordingly but it would use volatile functions which need recalculating each time so it will diminish your speed accordingly
Using a whole column reference like C:C is definitely very slow
Another option is to convert your original data to a table and update your formulas accordingly and this will shrink and grow automatically
Thanks for your immediate response, will surely check the table option too.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
What version of excel are you using
Take a look at dynamic defined range it uses offset and counta
 
Upvote 0
This is VBA code:
VBA Code:
Option Explicit
Sub report()
Dim lr&, lc&, i&, j&, rng, res(), cell As Range, s As String, st
Dim dic As Object, key
Set dic = CreateObject("Scripting.Dictionary")
With Sheets("Data")
    lr = .Cells(Rows.Count, "C").End(xlUp).Row ' last row
    rng = .Range("C2:Q" & lr).Value
    For i = 1 To UBound(rng)
        If Not dic.exists(rng(i, 1)) Then
            dic.Add rng(i, 1), rng(i, 4) & "|" & rng(i, 5) & "|" & rng(i, 7) & "|" & rng(i, 15) & "|" & rng(i, 2)
        Else
            dic(rng(i, 1)) = dic(rng(i, 1)) & "|" & rng(i, 2)
        End If
    Next
End With
Sheets("Report").Activate
i = 0: lc = Cells(1, Columns.Count).End(xlToLeft).Column ' last column
ReDim res(1 To dic.Count, 1 To lc)
rng = Range("A1", Cells(1, lc)).Value
For Each key In dic.keys
    st = Split(dic(key), "|"): i = i + 1: res(i, 1) = i: res(i, 2) = key
    res(i, 3) = st(0): res(i, 4) = st(1): res(i, 5) = st(2): res(i, 6) = st(3)
    For j = 7 To UBound(rng, 2)
        If InStr(1, dic(key), "|" & rng(1, j)) Then res(i, j) = rng(1, j)
    Next
Next
With Range("A2")
    .Resize(10000, 1000).ClearContents
    .Resize(dic.Count, lc).Value = res
End With
Set dic = Nothing
End Sub

Sample spreadsheet here
 
Upvote 0

dsubash​

If you want the code to populate the header in "report" then
Code:
Sub test()
    Dim a, i&, ii&, temp, dic(1) As Object
    Set dic(0) = CreateObject("Scripting.Dictionary")
    Set dic(1) = CreateObject("Scripting.Dictionary")
    With Sheets("data").[a1].CurrentRegion
        a = Application.Index(.Value2, Evaluate("row(1:" & .Rows.Count & ")"), [{4,3,17,7,9,6}])
        a(1, 1) = "Sl. No.": ReDim Preserve a(1 To UBound(a, 1), 1 To 1000)
    End With
    For i = 2 To UBound(a, 1)
        If dic(1)(a(i, 1)) = Empty Then
            dic(1)(a(i, 1)) = dic(1).Count + 6
            a(1, dic(1)(a(i, 1))) = a(i, 1)
        End If
        temp = a(i, 1)
        If dic(0)(a(i, 2)) = Empty Then
            dic(0)(a(i, 2)) = dic(0).Count + 1
            a(dic(0)(a(i, 2)), 1) = dic(0).Count
            For ii = 2 To 6
                a(dic(0)(a(i, 2)), ii) = a(i, ii)
            Next
        End If
        a(dic(0)(a(i, 2)), dic(1)(temp)) = temp
    Next
    With Sheets("report").[a1].CurrentRegion
        .ClearContents
        With .Resize(dic(0).Count + 1, dic(1).Count + 6)
            .Value = a
            .Rows(1).Font.Bold = True
        End With
        .Parent.Activate
    End With
End Sub

 
Upvote 0
Solution

dsubash​

If you want the code to populate the header in "report" then
Code:
Sub test()
    Dim a, i&, ii&, temp, dic(1) As Object
    Set dic(0) = CreateObject("Scripting.Dictionary")
    Set dic(1) = CreateObject("Scripting.Dictionary")
    With Sheets("data").[a1].CurrentRegion
        a = Application.Index(.Value2, Evaluate("row(1:" & .Rows.Count & ")"), [{4,3,17,7,9,6}])
        a(1, 1) = "Sl. No.": ReDim Preserve a(1 To UBound(a, 1), 1 To 1000)
    End With
    For i = 2 To UBound(a, 1)
        If dic(1)(a(i, 1)) = Empty Then
            dic(1)(a(i, 1)) = dic(1).Count + 6
            a(1, dic(1)(a(i, 1))) = a(i, 1)
        End If
        temp = a(i, 1)
        If dic(0)(a(i, 2)) = Empty Then
            dic(0)(a(i, 2)) = dic(0).Count + 1
            a(dic(0)(a(i, 2)), 1) = dic(0).Count
            For ii = 2 To 6
                a(dic(0)(a(i, 2)), ii) = a(i, ii)
            Next
        End If
        a(dic(0)(a(i, 2)), dic(1)(temp)) = temp
    Next
    With Sheets("report").[a1].CurrentRegion
        .ClearContents
        With .Resize(dic(0).Count + 1, dic(1).Count + 6)
            .Value = a
            .Rows(1).Font.Bold = True
        End With
        .Parent.Activate
    End With
End Sub

Thanks Fuji, Your code helped me save time.
 
Upvote 0

Forum statistics

Threads
1,225,699
Messages
6,186,523
Members
453,362
Latest member
zermrodrigues

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