Creating a report using VBA

Sarah12345

New Member
Joined
Jun 29, 2022
Messages
2
Platform
  1. Windows
Hi All,

I would like to create a report page which allows to user to choose the columns they want to include in a report and then specify a start and end date, click submit and this copies the relevant data and pastes it into a report page. There are multiple worksheets so I need the code to start with the first one and then run through the rest.

This is the current logic that I am working with;
- Go to the first worksheet (Can't specify a specific worksheet name as the user might change this, or add more worksheets)
- Copy the name within cell "A1" to use as a report sub-header
- Finds all the columns on the worksheet which has 'Yes' above them (This row will always be in the same position)
- Finds all the column titles on the worksheet that meets the report column criteria, copy the headers
- Finds the specific rows within the date range that the user specified (The start and end dates will be on the report worksheet, these can be in a fixed cell location)
- For each row where the data range meets the date range and the report column criteria, copy the row.
- Return the report sub-header, paste it into the report worksheet
- Insert the rows below the sub-header
- Continue with next sheet

I've attached a few images of the raw data and attached an image of the type of report that I want to create.

Ultimately the aim is to allow the workshop team to be able to enter and record all of their data they need to and run a report to capture the info they need to plan their works for the next month etc.

My biggest hurdle so far is finding the data I need based on the report columns, most code i've seen requires me to provide a specific name to look for e.g. "service_date"...

Any help or guidance to get me a bit further on my way will be appreciated!
 

Attachments

  • 1656664267232.png
    1656664267232.png
    40.5 KB · Views: 18
  • 1656665070949.png
    1656665070949.png
    32.3 KB · Views: 16
  • 1656665225403.png
    1656665225403.png
    17.8 KB · Views: 16

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
@Sarah12345
Is the column in each data sheet that holds the dates which are to be compared with the report date limits always headed 'Next Service Date' ?
 
Upvote 0
@Sarah12345 If the answer to the above is yes then below may be a solution?

'Report' sheet start and end date cells must be fixed.
Green range E3:F4 hold the date filter criteria. NB E3 and F3 Must be the exact string of the header of the date columns.
Maybe you can position your command button to hide the green cells?
Tabs can be in any order but code currently assumes all tabs other than the 'Report' tab are data tabs to export from.

So Data 1
Book2
ABCDEFGH
1Vehicle Maintenabce Tracker
2
3Columns to include with reports
4
5YesYesYesNoNoNoYesNo
6
7TypeMakeModelRegistrationService DateMileageNext Service DateNext Service Mileage
8VanCitroenBerlingN/A5/6/211234511/6/2145372
9LorryMercedesN/AABC 1231/1/22123457/23/2245372
10VanPeugeotBoxerN/A2/8/22123458/8/2245372
11VanFordTransitN/A12/20/21123456/20/2245372
12
Vehicles

Data 2
Book2
ABCDEFGHI
1Agitators Maintenance Tracker
2
3Columns to include with reports
4
5YesYesNoNoYesNoNoNoNo
6
7TypeMakeSerial No.Sevice DateNext Service DateEngineOil Filter1-FuelFilter1-Fuel Filter
8AgitatorCIFA599271/1/227/1/22DuetzW 940/5700 017FF5018
9AgitatorCIFA613062/8/228/8/22DuetzW 940/5700 017FF5018
10AgitatorCIFA6130612/20/216/20/22HatzW 940/5700 017FF5018
11
Agitators

Data 3
Book2
ABCDEFG
1Other Stuff Maintenabce Tracker
2
3Columns to include with reports
4
5YesYesYesNoYesYesNo
6
7StuffMore StuffModelService DateMileageNext Service DateNext Service Mileage
8Test 1qweqrA Stuff5/6/21123452/2/2245372
9Test 2abgeoB Stuff1/1/22123457/13/2245372
10Test 3qweqrB Stuff2/8/22123458/8/2245372
11Test 4tyurrA Stuff12/20/21123458/8/2245372
12
Stuff

Initial Report
Book2
ABCDEFG
1MaintenanceReport
2*Green Range Holds Filter Criterea
3Start Date1/2/90Next Service DateNext Service Date
4>=32875<=44762
5End Date7/20/22
6
7Stuff
8Loads of stuffLoads of stuffLoads of stuffLoads of stuff
9Loads of stuffLoads of stuffLoads of stuffLoads of stuff
10Loads of stuffLoads of stuffLoads of stuffLoads of stuff
11
12Remnants of previous report
13Loads of stuffLoads of stuffLoads of stuffLoads of stuff
14Loads of stuffLoads of stuffLoads of stuffLoads of stuff
15Loads of stuffLoads of stuffLoads of stuffLoads of stuff
16
17More flipping stuff
18Loads of stuffLoads of stuffLoads of stuffLoads of stuff
19Loads of stuffLoads of stuffLoads of stuffLoads of stuff
20
Report
Cell Formulas
RangeFormula
E4E4=">="&B3
F4F4="<="&B5


After code is run.
Book2
ABCDEFG
1MaintenanceReport
2*Green Range Holds Filter Criterea
3Start Date1/2/90Next Service DateNext Service Date
4>=32875<=44762
5End Date7/20/22
6
7Vehicle Maintenabce Tracker
8TypeMakeModelNext Service Date
9VanCitroenBerling11/6/21
10VanFordTransit6/20/22
11
12Agitators Maintenance Tracker
13TypeMakeNext Service Date
14AgitatorCIFA7/1/22
15AgitatorCIFA6/20/22
16
17Other Stuff Maintenabce Tracker
18StuffMore StuffModelMileageNext Service Date
19Test 1qweqrA Stuff123452/2/22
20Test 2abgeoB Stuff123457/13/22
21
Report
Cell Formulas
RangeFormula
E4E4=">="&B3
F4F4="<="&B5


Code to be housed in the Report sheet code module.
Currently there is no coded provision for failing to find data in a sheet but that can be sorted.

VBA Code:
Sub SarahTest()

Dim ReportRng As Range
Dim DataRng As Range
Dim CritereaRng As Range
Dim nxtr As Integer
Dim sht As Worksheet
Dim tws As Worksheet
Dim twb As Workbook

Set twb = ThisWorkbook
Set tws = twb.Sheets("Report")  'Edit if report sheet name differs
nxtr = tws.Range("A" & Rows.Count).End(xlUp).Row  'last row of report data
If nxtr < 8 Then nxtr = 10
tws.Range("A7:A" & nxtr).Rows.EntireRow.ClearContents  'clear existing report data
tws.Range("A7:A" & nxtr).Rows.EntireRow.ClearFormats  'Clear existing formats

For Each sht In twb.Sheets
    If sht.Name <> tws.Name Then    'process all sheets bar the report sheet
    
  nxtr = tws.Range("A" & Rows.Count).End(xlUp).Row + 2  'Next row for data title
  
  
    Set DataRng = sht.Range("A7").CurrentRegion   'data range of sheet
    Set ReportTL = tws.Range("A" & nxtr + 1) 'top left cell of destination range in report sheet
    Set CritereaRng = tws.Range("E3:F4")  'cells with date criterea for the filter
    
    ReportCol = 1  'initialise for column A
    tws.Range("A" & nxtr) = sht.Range("A1")  'data sheet title
    nxtr = nxtr + 1
    
    ' loop through row 5 of data to determine the 'Yes' columns
    For DataCol = 1 To DataRng.Columns.Count
      If sht.Cells(5, DataCol) = "Yes" Then
        ReportTL.Offset(0, ReportCol - 1) = sht.Cells(7, DataCol)  'if Yes add the data header to the report sheet
        ReportCol = ReportCol + 1  'increment for next report column
    End If
    Next DataCol
       
    Set ReportRng = tws.Range(Cells(nxtr, 1), Cells(nxtr, ReportCol - 1)) 'range of eport headers that will effect column sort
      
    DataRng.AdvancedFilter xlFilterCopy, CritereaRng, ReportRng
    End If
Next sht

End Sub

I hope that helps.
 
Upvote 0

Forum statistics

Threads
1,221,675
Messages
6,161,216
Members
451,691
Latest member
fjaimes042510

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