Solved

Excel Macro and Pivot Table, Part II

Posted on 2011-02-18
4
596 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
ID: 34969998
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
ID: 35017378
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

749 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