Interactively get a flat file list from a crosstab or Pivot Table (Excel)

The tool here shows how you can get a clean table or list from a crosstab in Excel for more flexible reporting.   Download the .xls file here: CrossTab-to-Flatfile-General .

(If you’re nervous about what that will do, you can instead inspect and load the code module file at the bottom of this post. )

Go to Sheet1.  This is an example from Angela Wolff, whose work I’ve developed here.  Her NHSExcel site explains the example and the objectives.  You can also read my earlier post below.

Push the button “Convert to Flat File”.  This will run a macro that builds a new worksheet just after the one you’re looking at, named “Sheet1_List“.  (It will destroy any earlier worksheet of that name.)

You’ll be asked to identify parts of the worksheet.  You can select the ranges with your mouse, or type them directly.

Using the sample worksheet Sheet1, these are your selections for the various prompts:

CrosstabToListUI-ColumnLabelBlock

The user selected the area in the dotted rectangle. The equivalent range is autofilled in the input box.

CrosstabToListUI-2-RowHeadingsBlock

‘Row headings’ are just one row deep. They provide a name for the domain of row labels beneath them.

CrosstabToListUI-3-CrosstabInitialRowSpecimen

Any single cell in the *top* crosstabulated data row is sufficient. (A crosstabulated value has both a row and a column label.)

CrosstabToListUI-4-Heading-ColumnLabelRow1

The column labels usually don’t get their own headings. You supply a word to cover the label values in the selected row.

CrosstabToListUI-5-Heading-ColumnLabelRow2

A second column label level in this case. Sometimes a single English noun is hard to find!

CrosstabToListUI-6-Heading-CrosstabValues

Finally, a name for the ‘dependent variable’ – the crosstabulated data. Usually a unit of some kind – e.g., hours, dollars, count.

If you want insight into what’s going on, or like direct assurance that nothing is dangerous, I’ve included a version of the source code file here.  (You can of course just go straight to the unprotected spreadsheet file and hit Ctrl-F11 to see this.)   The free WordPress.com account I’m using restricts the file types you can load.  That’s why internally I’ve attached the extra extension ‘.doc’ on the end.  This will be evident when you download or ‘save the link’: Module1.bas .  Rename it to Module1.bas before loading – or just open it, copy the text and paste into that module.
I’ve had the tool installed as an add-in, but this may be a bit of overkill for the amount of use it gets.

Truly reusable data: Convert an Excel crosstab to a flat file – a list

Just want the tool?

Here it is: CrossTab-to-Flatfile-General-TrueBackup .
I hope soon to post the version which I lost, that provides prompts to replace the constants.

What’s the issue?

Good for entry, bad for reporting

Often the way we enter data is not the way we want to view it later.

We might want to enter results for all students at the end of a series of assessments.  Perhaps we fill an Excel grid with the assessment events along one side and the students’ names on the other.  If there are more assessment events than students, the students may appear as columns and the events as rows.

Later, we want to look at an individual student’s history.  If we only have the spreadsheets in the form we have entered them, it is painful to do this.  We could consolidate the spreadsheets, but we want the students to appear as rows.  However, if we just swapped the rows and the columns (which you can do – it’s called transposing) there would still be a problem.  The columns would vary from period to period; it’d be hard to match like-with-like and to identify new items.

Agnostic records – the list

The best way to deal flexibly with data – for general storage purposes – is by making what Excel calls a list or a table.  Data people call it a ‘flat file’.  Every item of information that might be found in the middle of the grid gets its own row.  And every piece of information that helps to identify that ‘data item’ appears also on that row.   It’s a very simple format.  Whatever row or column headings were in the original spreadsheet become column headings in the list.  The very last column contains the ‘data item’.

This makes for a very long list – not much good as any particular report – but great as fodder for a computer to flexibly produce any report!   Database programs like Access love them.  Even in Excel, the power of Pivot Tables depend on them.

A macro solution

So how do you convert a conventional  ‘crosstabulated’ report into a flat file – an Excel ‘list’?

It requires a macro.  I provide one here, which I adapted and developed from great initial work by Angela Wolff which I found on the site NHS Excel  – http://nhsexcel.com/excel-pivot-table-crosstab-flat-list/ .

I recommend her article for the low-down on how to do some early preparation of your table.  For example, if there are blanks on rows where it’s assumed you should read ‘as above’, you want to replace these with the exact information.  (The same is true for column headings that notionally ‘straddle’ other headings below them: instead, each heading must be explicitly listed.  Unmerge any merged columns first!)

My work makes it easier to adapt the macro to your specific situation.  You may pass this to your information technology section if you aren’t comfortable with the VBA interface (Control-F11 to get in and back out).  (I wrote a version with a user interface, and promptly lost it in trying to preserve backups!  I hope to load a revision soon.)

Download this file: CrossTab-to-Flatfile-General-TrueBackup.  It’s an old-style Excel spreadsheet (.xls file).

There’s a set of constants which you should set up for your situation.  In Angela’s example, there are two rows of column-headed-data, and three columns of ‘static’ data that appear under new rows.

' SET THESE CONSTANTS ...

Const SOURCE_WORKSHEET_NAME = "Sheet 1" ' Specify the name of the crosstab worksheet in the workbook
Const SOURCE_DATA_ROW_INITIAL = 3 'Where data begin in the source crosstab (multiple heading levels => higher row number)
Const SOURCE_ROW_DATA_HEADING_ROW = 2 ' Row where headings for the row-repeating ('static') data are found in the source crosstab. Usually immediately above first such data.
Const SOURCE_ROW_DATA_COLUMN_INITIAL = "A" 'Where row-repeating ('static') data begin in the source crosstab (generally column "A")
Const SOURCE_ROW_DATA_COLUMN_FINAL = "C" 'Last column where row-repeating ('static') data appear in the source crosstab.
Const SOURCE_COLUMN_DATA_HEADING_BLOCK = "D1:K2" ' The region of values that appear as headers across the body of the crosstab. (Derivable in standard cases, assuming contiguity and start from Row 1. Depth = SOURCE_DATA_ROW_INITIAL - 1. Width = Rightmost cell - ordinal of (SOURCE_ROW_DATA_COLUMN_FINAL)). But not if these 'column heading' data each have their own headings: a blank row will be required to house the original row headings.
 

You must also make a list of headings for your target spreadsheet, according to the structure outlined in the code.

 
wsList.Range("A1:F1") = Array("NAME", "PROJECT", "TYPE", "WEEK", "PLAN/ACTUAL", "HOURS") ' Modify as outlined above.

A trick employed

‘Under the hood’ people may be interested in how I reorganised the processing of what was originally ‘column heading data’.  (In the example, the data labelling weeks and plan/actual categories).  I used the same principle as had already been applied for the ‘static data’: if your target paste region in Excel is a precise multiple of your source, Excel will repeat the pattern to fill the target.  We can regard the whole column header block as a matrix to be transposed within the target list.  The same block will recur (repeat) for each row of data in our original sheet.  So there’s no need to copy more than once: simply paste-transpose the block into a region of the right size and position in the destination list.  This is a single action, outside the row-by-row ‘loop’ processing.  I choose to do it at the end.

Editing a handwritten essay in Word

Really?  If you’re like me, and your typing is both faster and neater than your handwriting, you may be tempted to use a computer to mark student essays.  There are other advantages, too: word completion, footnoting, linking can make both production of and student use of an e-marked document more effective.

But if the work was handwritten, how do we navigate it?  How do we mix typed and handwritten text?

I’ve written a tool to help.

Microsoft Publisher’s Catalog Merge

You can use Microsoft Publisher to print game cards.  It’s much easier to set up a format in Publisher, then concentrate on your individual records by typing them into a conventional grid – say in Access, Excel, or even a Word table.

This blog entry doesn’t hold your hand to get you there.  It assumes you’re already familiar with Mail Merge and data sources.

It’s actually a stop-gap entry, to save myself the embarrassment of seeing a “Hello World!” default entry from WordPress.com still in my blog!  (With this entry, I am also testing an RSS-to-Email service I have just subscribed to.  That should be the subject of another post!)