+ Reply to Thread
Results 1 to 24 of 24

Vba to create report

Hybrid View

phbryan Vba to create report 03-07-2018, 12:01 PM
Mumps1 Re: Vba to create report 03-07-2018, 01:11 PM
phbryan Re: Vba to create report 03-08-2018, 12:36 PM
Mumps1 Re: Vba to create report 03-08-2018, 12:58 PM
phbryan Re: Vba to create report 03-08-2018, 01:49 PM
Mumps1 Re: Vba to create report 03-08-2018, 01:56 PM
phbryan Re: Vba to create report 03-08-2018, 02:22 PM
Mumps1 Re: Vba to create report 03-08-2018, 03:24 PM
phbryan Re: Vba to create report 03-09-2018, 11:25 AM
Mumps1 Re: Vba to create report 03-09-2018, 03:25 PM
phbryan Re: Vba to create report 04-11-2018, 11:59 AM
Mumps1 Re: Vba to create report 04-12-2018, 01:15 PM
phbryan Re: Vba to create report 04-12-2018, 01:36 PM
Mumps1 Re: Vba to create report 04-12-2018, 02:28 PM
phbryan Re: Vba to create report 04-12-2018, 02:33 PM
Mumps1 Re: Vba to create report 04-12-2018, 03:00 PM
phbryan Re: Vba to create report 04-12-2018, 03:02 PM
Mumps1 Re: Vba to create report 04-12-2018, 03:10 PM
phbryan Re: Vba to create report 04-12-2018, 03:19 PM
Mumps1 Re: Vba to create report 04-13-2018, 09:09 AM
phbryan Re: Vba to create report 04-13-2018, 01:19 PM
Mumps1 Re: Vba to create report 04-13-2018, 01:50 PM
phbryan Re: Vba to create report 04-17-2018, 08:47 AM
Mumps1 Re: Vba to create report 04-17-2018, 10:08 AM
  1. #1
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Vba to create report

    Hello,

    Attached is a spreadsheet of data readings from a piece of equipment. I want a way to view min, max, and average based on a date range. Values in column B indicate a specific data reading. Values in column C are the dates the values were recorded. Values in column D is the recorded measurement. So, for example, I'd like to look at a reading in column B, date range in column C, and the min, max, and average of the values in D based on the first two requirements.

    I know this is possible with AGGREGATE and auto filter, but can i do it with VBA and generate a report for all values in B between a certain date range, possibly outputted to a pdf or another spreadsheet/workbook?

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try:
    Sub CreateReport()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim sDate As String
        Dim eDate As String
        sDate = InputBox("Please enter the start date in format mm/dd/yyyy", "Enter Start Date", "mm/dd/yyyy")
        eDate = InputBox("Please enter the end date in format mm/dd/yyyy", "Enter End Date", "mm/dd/yyyy")
        ActiveSheet.ListObjects("Table_Query_from_MS_Access_Database3").Range. _
            AutoFilter Field:=3, Criteria1:=">=" & CDate(sDate), Operator:=xlAnd, Criteria2:="<=" & CDate(eDate)
        Range("C1") = WorksheetFunction.Min(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Range("C2") = WorksheetFunction.Max(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Range("C3") = WorksheetFunction.Average(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Application.ScreenUpdating = True
    End Sub
    Last edited by Mumps1; 03-07-2018 at 01:33 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    That works great for quickly sorting the information by date and creating min, max, average. Can it go further and create a table for each of the values in B12:B and each of their min, max, averages? I put an example table in sheet2. I trying to have that populate automatically
    Attached Files Attached Files

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try:
    Sub CreateReport()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim sDate As String
        Dim eDate As String
        sDate = InputBox("Please enter the start date in format mm/dd/yyyy", "Enter Start Date", "mm/dd/yyyy")
        eDate = InputBox("Please enter the end date in format mm/dd/yyyy", "Enter End Date", "mm/dd/yyyy")
        ActiveSheet.ListObjects("Table_Query_from_MS_Access_Database3").Range. _
            AutoFilter Field:=3, Criteria1:=">=" & CDate(sDate), Operator:=xlAnd, Criteria2:="<=" & CDate(eDate)
        Sheets("Sheet2").Columns("H").ClearContents
        Sheets("Sheet2").Range("I4:K4").ClearContents
        Sheets("Sheet2").Cells(1, 8) = sDate
        Sheets("Sheet2").Cells(2, 8) = eDate
        Range("B12:B" & LastRow).SpecialCells(xlCellTypeVisible).Copy
        Sheets("Sheet2").Cells(4, 8).PasteSpecial xlPasteValues
        Sheets("Sheet2").Cells(4, 9) = WorksheetFunction.Min(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Sheets("Sheet2").Cells(4, 10) = WorksheetFunction.Max(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Sheets("Sheet2").Cells(4, 11) = WorksheetFunction.Average(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    I copied that code into a new module of the attached workbook and ran it. It seems to just be coping all of column B over to sheet2 column H.

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    When I try it, it copies the values from column B based on the start and end dates entered, the start and end dates in I1 and I2 and the min, max and average in I4:K4. Do you not get the same thing?

  7. #7
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    Yes it does do that
    I guess I wasn't clear enough in my description. Sorry.
    So, what i'm trying to do on sheet2 is get the min, max, average, for value a2 (drop down filter on ToolName) in I4:K4, then the min, max, average for a3 in I5:K5, and so on until I've done all values in the drop down filter... All within my initial start/end dates

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try this macro. It also removes the duplicate Tool Names in Sheet2. I assumed that is what you wanted. If not, just delete the "RemoveDuplicates" line of code.
    Sub CreateReport()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim ToolName As Range
        Dim sDate As String
        Dim eDate As String
        sDate = InputBox("Please enter the start date in format mm/dd/yyyy", "Enter Start Date", "mm/dd/yyyy")
        eDate = InputBox("Please enter the end date in format mm/dd/yyyy", "Enter End Date", "mm/dd/yyyy")
        ActiveSheet.ListObjects("Table_Query_from_MS_Access_Database3").Range. _
            AutoFilter Field:=3, Criteria1:=">=" & CDate(sDate), Operator:=xlAnd, Criteria2:="<=" & CDate(eDate)
        Sheets("Sheet2").Columns("H").ClearContents
        Sheets("Sheet2").Range("I4:K" & Range("K" & Rows.Count).End(xlUp).Row).ClearContents
        Sheets("Sheet2").Cells(1, 8) = sDate
        Sheets("Sheet2").Cells(2, 8) = eDate
        Range("B12:B" & LastRow).SpecialCells(xlCellTypeVisible).Copy
        Sheets("Sheet2").Cells(4, 8).PasteSpecial xlPasteValues
        For Each ToolName In Range("B12:B" & LastRow).SpecialCells(xlCellTypeVisible)
            ActiveSheet.ListObjects("Table_Query_from_MS_Access_Database3").Range. _
            AutoFilter Field:=2, Criteria1:=ToolName
            Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "I").End(xlUp).Offset(1, 0) _
                = WorksheetFunction.Min(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
            Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "J").End(xlUp).Offset(1, 0) _
                = WorksheetFunction.Max(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
            Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "K").End(xlUp).Offset(1, 0) _
                = WorksheetFunction.Average(Range("D12:D" & LastRow).SpecialCells(xlCellTypeVisible))
        Next ToolName
        Sheets("Sheet2").Cells.RemoveDuplicates Columns:=Array(8), Header:=xlNo 'removes entire row
        Range("A11").AutoFilter
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    Data is being moved into sheet2 it does not seem to be correct. The average in the first sheet should equal the one in the table on sheet2, but it does not.
    Attached Files Attached Files

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Have a look at the attached file. When I filter on a2 with the dates between 02/01/2018 and 02/02/2018, which are the dates that you used, I get the results shown in sheet "TnG-000". The Min, Max and Avg matches the data for a2 in Sheet2. Please note that I have made a few changes to the macro, so please use the version in the attached file.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    Thank you for the help and sorry for the delayed response
    The code is working great
    I forgot something that might complicate things, I need to create four tables instead of one based on what is column F
    See attachment
    Thank You!
    Attached Files Attached Files

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try the attached file. There may be a more elegant way of doing this but this is what worked for me. Just click the "Create Report" button. Give the macro a chance to run depending on how much data it has to process.
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    I'm getting a runtime error. Object variable or with block not set on this line:
    If Sheets("TnG-000").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 11 Then

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    What start and end dates did you enter?

  15. #15
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    Tried a few

    12/1/17 -> 1/1/18
    1/1/18 -> 2/1/18
    2/1/18 -> 3/1/18

    Got the same error for all

  16. #16
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    I think that if you enter a date that doesn't exist, you get the error. I've modified the macro to warn you if you enter a non-existent date. Try the attached file and enter 01/06/2018 and 01/08/2018.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    For some reason the code runs but only when the table is selected.
    For example, if i select A11 then run the macro, no error.
    But if i select A10 and run, i get the error

    Any idea why that is happening?

  18. #18
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    I have no idea!!! Let's force it to select A11. Place this line of code:
    Sheets("TnG-000").Range("A11").Select
    above this line:
    sDate = InputBox("Please enter the start date in format mm/dd/yyyy", "Enter Start Date", "mm/dd/yyyy")

  19. #19
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    LOL
    Besides that this code is working awesomely! You rock!
    One final thing, each of the four tables produced aren't in the same order. Is there a quick fix to sort them A->Z?

  20. #20
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try the attached file.
    Attached Files Attached Files

  21. #21
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    The code works great but i've noticed something while playing around with the code
    If a date range doesn't contain 1a, the code doesn't carry data over. For example, try date range 3/1/18 - 3/3/18 in the attached file (more data in this file. up to 4/12/18)
    Attached Files Attached Files

  22. #22
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try the attached.
    Attached Files Attached Files

  23. #23
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Vba to create report

    That works
    for some reason the first table isnt sorting?

  24. #24
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: Vba to create report

    Try the attached.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Create a running total - create a "pretty" report
    By excelnewbie1234567 in forum Excel General
    Replies: 1
    Last Post: 03-08-2017, 11:41 AM
  2. VBA the best way to create report?
    By ClareLou in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-13-2016, 05:27 AM
  3. [SOLVED] How to create this report
    By sarahqputra in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-17-2014, 02:12 AM
  4. Replies: 2
    Last Post: 06-29-2011, 04:35 PM
  5. Create a Report
    By bhallam in forum Access Tables & Databases
    Replies: 5
    Last Post: 03-18-2011, 03:21 PM
  6. Create Report
    By Ken@Excel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-24-2009, 07:40 AM
  7. trying to create a report
    By BudS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-02-2006, 09:30 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