Code to produce pivot like summary

JonRowland

Active Member
Joined
May 9, 2003
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hi Forum experts. I am hoping someone can help with some VBA code for me to do the following, as all my attempts have failed :(

For the purpose of this post I have simplified my example data but will likely have around 6-7 time periods and x amount of CallCodes.

What I start with is a list of Time Periods and CallCode that occurred within the period, as below

[table="width: 500, class: grid, align: center"]
[tr]
[td]Time Period[/td]
[td]CallCode[/td]
[/tr]
[tr]
[td]00:00 - 05:59[/td]
[td]Y24[/td]
[/tr]
[tr]
[td]06:00 - 11:59[/td]
[td]X01[/td]
[/tr]
[tr]
[td]15:00 - 20:59[/td]
[td]D09[/td]
[/tr]
[tr]
[td]15:00 - 20:59[/td]
[td]X01[/td]
[/tr]
[/table]

What I am trying to do is produce a result like this

[table="width: 500, class: grid, align: center"]
[tr]
[td]CallCode[/td]
[td]00:00 - 05:59
[/td]
[td]06:00 - 11:59[/td]
[td]15:00 - 20:59[/td]
[/tr]
[tr]
[td]Y24[/td]
[td]1[/td]
[td]0[/td]
[td]0[/td]
[/tr]
[tr]
[td]X01[/td]
[td]0[/td]
[td]1[/td]
[td]1[/td]
[/tr]
[tr]
[td]D09[/td]
[td]0[/td]
[td]0[/td]
[td]1[/td]
[/tr]
[/table]

Before anyone asks why not use a Pivot Table this is part of a bigger routine and what to include this automated part.
 
Re: Help

hiker95 - one more question I have some CallCodes that being with 00

When we get to <code>.Cells(2, 26).Resize(UBound(cc, 1)).Value = cc</code> the leading Zeros are dropped. I've tried but can't see what I need to do to keep the leading zeros.

Thx
Jon
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Re: Help

JonRowland,

hiker95 - one more question I have some CallCodes that being with 00

When we get to .Cells(2, 26).Resize(UBound(cc, 1)).Value = cc the leading Zeros are dropped. I've tried but can't see what I need to do to keep the leading zeros.

Can we have a screenshot, like your original reply, that contains some of the CallCodes that begin with 00?
 
Last edited:
Upvote 0
Re: Help

hiker95

Can't give screenshot at the moment but example of codes would be;

001234
03456
00987653

HTH
 
Upvote 0
Re: Help

JonRowland,

Here is an updated macro for you to consider:

Sample raw data, and, results:


Excel 2007
ABCDEFG
1Time PeriodCallCodeCall Code00:00 - 05:5906:00 - 11:5915:00 - 20:59
200:00 - 05:59Y24Y24100
300:00 - 05:59001234001234100
406:00 - 11:59X01X01011
506:00 - 11:590345603456020
606:00 - 11:5903456D09001
715:00 - 20:59D0900987653001
815:00 - 20:59X01
915:00 - 20:5900987653
10
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReorgTimePeriodCallCodeV2()
' hiker95, 03/06/2015, ME840231
Dim c As Range, rng As Range, tp, cc
Dim r As Range, d As Range
With ActiveSheet
  .Cells(1, 4) = "Call Code"
  Set rng = .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)
  With CreateObject("Scripting.Dictionary")
    For Each c In rng
      If c <> "" Then
        If Not .Exists(c.Value) Then
          .Add c.Value, c.Value
        End If
      End If
    Next
    cc = Application.Transpose(Array(.Keys))
  End With
  With .Cells(2, 4).Resize(UBound(cc, 1))
    .NumberFormat = "@"
    .Value = cc
  End With
  Set rng = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
  With CreateObject("Scripting.Dictionary")
    For Each c In rng
      If c <> "" Then
        If Not .Exists(c.Value) Then
          .Add c.Value, c.Value
        End If
      End If
    Next
    tp = Application.Transpose(Array(.Keys))
  End With
  .Cells(1, 5).Resize(, UBound(tp, 1)) = Application.Transpose(tp)
  .Columns(4).Resize(, UBound(tp) + 1).AutoFit
  .Range("E2").Resize(UBound(cc), UBound(tp)) = 0
  For Each c In .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    Set d = .Columns(4).Find(c.Value, LookAt:=xlWhole)
    Set r = .Rows(1).Find(c.Offset(, -1).Value, LookAt:=xlWhole)
    If (Not d Is Nothing) * (Not r Is Nothing) Then
      .Cells(d.Row, r.Column) = .Cells(d.Row, r.Column) + 1
    End If
  Next c
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgTimePeriodCallCodeV2 macro.
 
Upvote 0
Re: Help

Apologies hiker95...I forgot to response.

That work and is fantastic and I've been able to slip it into my main routine and get the result I want.

Many thanks for your help. :beerchug:
 
Upvote 0
Re: Help

JonRowland,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Re: Help

Sorry to bump this after so long. Hopefully Hiker95 can help. But I am trying to work out how to get this code to differentiate between lower and upper case and to treat them as separate entities.

For example I have y24 and Y24. So this ends up putting my count figures in the first y24 and nothing in Y24.

Anyone any clues?

Jon



JonRowland,

Here is an updated macro for you to consider:

Sample raw data, and, results:


Excel 2007
ABCDEFG
1Time PeriodCallCodeCall Code00:00 - 05:5906:00 - 11:5915:00 - 20:59
200:00 - 05:59Y24Y24100
300:00 - 05:59001234001234100
406:00 - 11:59X01X01011
506:00 - 11:590345603456020
606:00 - 11:5903456D09001
715:00 - 20:59D0900987653001
815:00 - 20:59X01
915:00 - 20:5900987653
10
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReorgTimePeriodCallCodeV2()
' hiker95, 03/06/2015, ME840231
Dim c As Range, rng As Range, tp, cc
Dim r As Range, d As Range
With ActiveSheet
  .Cells(1, 4) = "Call Code"
  Set rng = .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)
  With CreateObject("Scripting.Dictionary")
    For Each c In rng
      If c <> "" Then
        If Not .Exists(c.Value) Then
          .Add c.Value, c.Value
        End If
      End If
    Next
    cc = Application.Transpose(Array(.Keys))
  End With
  With .Cells(2, 4).Resize(UBound(cc, 1))
    .NumberFormat = "@"
    .Value = cc
  End With
  Set rng = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
  With CreateObject("Scripting.Dictionary")
    For Each c In rng
      If c <> "" Then
        If Not .Exists(c.Value) Then
          .Add c.Value, c.Value
        End If
      End If
    Next
    tp = Application.Transpose(Array(.Keys))
  End With
  .Cells(1, 5).Resize(, UBound(tp, 1)) = Application.Transpose(tp)
  .Columns(4).Resize(, UBound(tp) + 1).AutoFit
  .Range("E2").Resize(UBound(cc), UBound(tp)) = 0
  For Each c In .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    Set d = .Columns(4).Find(c.Value, LookAt:=xlWhole)
    Set r = .Rows(1).Find(c.Offset(, -1).Value, LookAt:=xlWhole)
    If (Not d Is Nothing) * (Not r Is Nothing) Then
      .Cells(d.Row, r.Column) = .Cells(d.Row, r.Column) + 1
    End If
  Next c
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgTimePeriodCallCodeV2 macro.
 
Upvote 0
Re: Help

Re: Help
Sorry to bump this after so long. Hopefully Hiker95 can help. But I am trying to work out how to get this code to differentiate between lower and upper case and to treat them as separate entities.

For example I have y24 and Y24. So this ends up putting my count figures in the first y24 and nothing in Y24.

Anyone any clues?

Jon

JonRowland,

Can you supply a screenshot that contains lower, and, upper Y, examples?

If you are not able to provide screenshots, then:

You can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com
 
Upvote 0
Re: Help

Hi,

Hopefully, this simple example will explain what the end result I am looking for. There will be duplicates and some of the values will be pure text, some numbers and some alpha numeric.

Jon


Book1
ABCDEFGH
1ABCDEFG
21Time PeriodCallCodeCall Code00:00 - 05:5906:00 - 11:5915:00 - 20:59
3200:00 - 05:59Y24Y24100
4300:00 - 05:5912341234100
5406:00 - 11:59X01X01111
6506:00 - 11:5934563456020
7606:00 - 11:593456D09001
8715:00 - 20:59D09987653001
9815:00 - 20:59X01x01010
10915:00 - 20:59987653
111006:00 - 11:59x01
121100:00 - 05:59X01
Sheet1
 
Upvote 0
Re: Help

JonRowland,

I have not been able to duplicate your results in your reply #19.

Maybe MickG, a Guru with the Scripting.Dictionary, will see your request.
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,020
Members
452,542
Latest member
Bricklin

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