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.