Need assistance with crosstab query

tmcrouse

Board Regular
Joined
Apr 10, 2012
Messages
121
I am not exactly how to get this crosstab query to work how I need it to. The below is the data have and need: I tried a crosstab but the results were not like my data need. I am missing something not sure what.

Data Have
last
first
add
city
state
zip
calldate
Jones
Sarah
111 Walnut
Spencer
WA
10101
10/10/2014
Jones
Sarah
111 Walnut
Spencer
WA
10101
10/11/2014
Jones
Sarah
111 Walnut
Spencer
WA
10101
10/12/2014
Jones
Zoe
114 Walnut
Spencer
WA
10104
10/9/2014
Jones
Zoe
114 Walnut
Spencer
WA
10104
10/10/2014
Jones
Zoe
114 Walnut
Spencer
WA
10104
10/15/2014
JOnesZoe114 WalnutSpencerWA1010410/16/2014
Data Need
last
first
add
city
state
zip
CallDate1
CallDate2
CallDate3
Calldate4
Jones
Sarah
111 Walnut
Spencer
WA
10101
10/10/2014
10/11/2014
10/12/2014
Jones
Zoe
114 Walnut
Spencer
WA
10104
10/9/2014
10/10/2014
10/15/2014
10/16/2014

<TBODY>
</TBODY>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
My crosstab query is not outputting any value information

I am trying to create a crosstab query and the output is stating that it cannot be aggregated. I am not sure what I am doing wrong. I have over 10k recrods and here is an example of the data have and need:

DATA HAVE
LASTFIRSTCALLRESULTS
DOESARAH10/15/2014NO ANSWER
DOESARAH10/16/2014NO ANSWER
DOESARAH10/17/2014NO ANSWER
DOESARAH10/18/2014LEFT MSG
DOESARAH10/19/2014REACHED
LANCEBRANDY10/15/2014NO ANSWER
LANCEBRANDY10/16/2014NO ANSWER
LANCEBRANDY10/17/2014NO ANSWER
LANCEBRANDY10/18/2014LEFT MSG
LANCEBRANDY10/19/2014REACHED
MIRIAMSUE10/15/2014NO ANSWER
MIRIAMSUE10/16/2014NO ANSWER
MIRIAMSUE10/17/2014NO ANSWER
MIRIAMSUE10/18/2014LEFT MSG
MIRIAMSUE10/19/2014REACHED

<COLGROUP><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2699" width=77><COL style="WIDTH: 43pt; mso-width-source: userset; mso-width-alt: 1978" width=57><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2862" width=82><TBODY>
</TBODY>


DATA NEED
LASTFIRSTCALL1RESULTS1CALL2RESULTS2CALL3RESULTS3CALL4RESULTS4CALL5RESULTS5
DOESARAH10/15/2014NO ANSWER10/16/2014NO ANSWER10/17/2014NO ANSWER10/18/2014LEFT MSG10/29/2014REACHED
LANCEBRANDY10/16/2014NO ANSWER10/17/2014NO ANSWER10/18/2014NO ANSWER10/19/2014LEFT MSG10/30/2014REACHED
MIRIAMSUE10/17/2014NO ANSWER10/18/2014NO ANSWER10/19/2014NO ANSWER10/20/2014LEFT MSG10/31/2014REACHED

<COLGROUP><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2699" width=77><COL style="WIDTH: 43pt; mso-width-source: userset; mso-width-alt: 1978" width=57><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2862" width=82><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2862" width=82><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2862" width=82><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 49pt; mso-width-source: userset; mso-width-alt: 2280" width=65><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2676" width=77><COL style="WIDTH: 49pt; mso-width-source: userset; mso-width-alt: 2257" width=65><TBODY>
</TBODY>

I have tried:

Rich (BB code):
TRANSFORM First(com.call) AS call
Rich (BB code):
Rich (BB code):
Rich (BB code):
SELECT com.first, com.last, com.call, com.results
FROM com<o:p></o:p>
GROUP BY com.call, com.result
PIVOT com.call, com.result;



and
Rich (BB code):
Rich (BB code):
Rich (BB code):
Rich (BB code):
TRANSFORM (com.results) AS results
SELECT cobc.Last, com.first, com.call, com.results
FROM com
GROUP BY com.Last, com.first, com.call, com.results
PIVOT com;
 
Upvote 0
tmcrouse,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?

Here is a macro solution for you to consider.

Sample raw data worksheet Sheet1 (before, and, after the macro)(you can change the raw data worksheet name in the macro):


Excel 2007
ABCDEFG
1lastfirstaddcitystatezipcalldate
2JonesSarah111 WalnutSpencerWA1010110/10/2014
3JonesSarah111 WalnutSpencerWA1010110/11/2014
4JonesSarah111 WalnutSpencerWA1010110/12/2014
5JonesZoe114 WalnutSpencerWA1010410/9/2014
6JonesZoe114 WalnutSpencerWA1010410/10/2014
7JonesZoe114 WalnutSpencerWA1010410/15/2014
8JOnesZoe114 WalnutSpencerWA1010410/16/2014
9
Sheet1



Excel 2007
ABCDEFG
1lastfirstaddcitystatezipcalldate
2JonesSarah111 WalnutSpencerWA1010110/10/2014
3JonesSarah111 WalnutSpencerWA1010110/11/2014
4JonesSarah111 WalnutSpencerWA1010110/12/2014
5JonesZoe114 WalnutSpencerWA1010410/9/2014
6JonesZoe114 WalnutSpencerWA1010410/10/2014
7JonesZoe114 WalnutSpencerWA1010410/15/2014
8JonesZoe114 WalnutSpencerWA1010410/16/2014
9
Sheet1


And, after the macro in a new worksheet Results (you can change the results worksheet name in the macro in two locations)


Excel 2007
ABCDEFGHIJK
1lastfirstaddcitystatezipCallDate1CallDate2CallDate3CallDate4
2JonesSarah111 WalnutSpencerWA1010110/10/201410/11/201410/12/2014
3JonesZoe114 WalnutSpencerWA1010410/9/201410/10/201410/15/201410/16/2014
4
Results


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).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ConsolidateData()
' hiker95, 03/31/2015, ME845570
Dim w1 As Worksheet, wr As Worksheet
Dim lr As Long, r As Long, nr As Long, n As Long, lc As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   ''<-- change the raw data sheet name here
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"   ''<-- you can change the results sheet name here
Set wr = Sheets("Results")   ''<-- you can change the results sheet name here
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(, 6).Value = w1.Cells(1, 1).Resize(, 6).Value
End With
With w1
  .Activate
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  With .Range("A2:D" & lr)
    .Value = Evaluate(Replace("If(Len(@),Proper(@),"""")", "@", .Address))
  End With
  With .Range("E2:E" & lr)
    .Value = Evaluate(Replace("If(Len(@),Upper(@),"""")", "@", .Address))
  End With
  With .Range("H2:H" & lr)
    .Formula = "=A2&B2&C2&D2&E2&F2"
    .Value = .Value
  End With
  For r = 2 To lr
    nr = wr.Cells(wr.Rows.Count, "A").End(xlUp).Row + 1
    n = Application.CountIf(.Columns(8), .Cells(r, 8).Value)
    If n = 1 Then
      wr.Cells(nr, 1).Resize(, 7).Value = .Cells(r, 1).Resize(, 7).Value
    ElseIf n > 1 Then
      wr.Cells(nr, 1).Resize(, 6).Value = .Cells(r, 1).Resize(, 6).Value
      wr.Cells(nr, 7).Resize(, r + n - 1).Value = Application.Transpose(.Range("G" & r & ":G" & r + n - 1))
    End If
    r = r + n - 1
  Next r
  .Range("H2:H" & lr).ClearContents
End With
With wr
  .Activate
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  On Error Resume Next
  With Range(Cells(2, 7), Cells(nr, lc))
    .SpecialCells(xlCellTypeConstants, xlErrors).ClearContents
  End With
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  .Range(.Cells(2, 7), .Cells(nr, lc)).NumberFormat = "m/d/yyyy"
  With .Cells(1, 7).Resize(, lc - 6)
    .Formula = "=""CallDate"" & Column() - 6"
    .Value = .Value
  End With
  .Columns.AutoFit
End With
Application.ScreenUpdating = True
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 ConsolidateData macro.
 
Upvote 0

Forum statistics

Threads
1,221,893
Messages
6,162,662
Members
451,781
Latest member
DylantheD

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