Solved

Excel Macro and Pivot Table, Part II

Posted on 2011-02-18
4
592 Views
Last Modified: 2012-05-11
My boss asked the following for a macro that creates a pivot table in Excel.  "Is it possible to get 1st login and last log out of each day on the line to the right of totals for each day?"  What adds complexity is that there might not be the same # of rows each time for the pivot table, depending on how many employees actually worked that day.  See attachments:  one is the pivot table and one has the macro within it.  Thanks.

P.S. - And why is the first file so big?  Is it the Pivot table?



LogonReport.xls
LogonReportStarter2.xls
0
Comment
Question by:rvfowler2
4 Comments
 
LVL 2

Author Comment

by:rvfowler2
Comment Utility
Thanks, tha texplains it.  I meant for it to be assigned to Excel and VB script.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
Comment Utility
Is this what you had in mind:
Sub BuildPivot()
   Dim pc As PivotCache
   Dim PT As PivotTable
   Set pc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "'" & ActiveSheet.Name & "'!" & Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1))
   Set PT = pc.CreatePivotTable(TableDestination:=ActiveSheet.Range("K3"), TableName:="PivotTable1", _
                                DefaultVersion:=xlPivotTableVersion10)
   With PT
      With .PivotFields("Userid")
         .Orientation = xlRowField
         .Position = 1
      End With
      With .PivotFields("Day")
         .Orientation = xlRowField
         .Position = 2
      End With
      .AddDataField .PivotFields("Hours"), "Total Hours", xlSum
      .AddDataField .PivotFields("Time"), "Earliest Time", xlMin
      With .DataPivotField
         .Orientation = xlColumnField
         .Position = 1
      End With
      .AddDataField .PivotFields("Time"), "Latest Time", xlMax
      .PivotFields("Total Hours").NumberFormat = "hh:mm"
      .PivotFields("Earliest Time").NumberFormat = "hh:mm"
      .PivotFields("Latest Time").NumberFormat = "hh:mm"
   End With
End Sub

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now