This article concerns fast formatting of data in MS Excel. The methods described were originally developed to apply various formatting to MS Excel worksheets created from MS Access using Automation. However all these methods work as well when applied in MS Excel itself.
Creating worksheets
Formatting text
Creating Outline and Indent
Drawing Hierarchical Headers
There is common method to add worksheet to workbook. However it can save much of your time if you know apriory how much pages there will be in the workbook and create a book with required number of pages.
Sample:
|
|
So now we have workbook WB with 20 worksheets which we can access using WB.Worksheets(
N ) where N is 1 based number of worksheet.
However it may appear faster to use the following technique: if you have workbook
with many worksheets of the same format, which differs only in data, you may
create and format one sheet then use
Worksheet.Copy After:=Sheet1 and then wipe old data and apply new. If time is
critical both ways should be tested.
Notice, that each worksheet has a Name property (which is what you see on tab)
and CodeName property which is inner name for worksheet (you can see code name
in VBA Editor for example). So, when you copy worksheet, the new code name will
be the old one added with "1" string. Doing this many times may lead
to error as described here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;236299
Text formatting is not a problem since you understood MS excel object model.
Few tips to make it faster:
If possible, don't format worksheet cell by cell, especially conditional format.
Lots of time wasted to read cell value and activate next cell. So, if the whole
range of cells has the same format, it's great time-saving to apply this format
to whole range, not to single cell. You can also use FillAcrossSheets method
to copy formats to all the (or some of the) sheets in workbook.
|
|
The most powerful way to apply formatting to unbound ranges is to use AutoFilter.
Lets say you have worksheet object WS and square range on it with top left corner
x,y and m and n width and height.
|
|
Let's explain the code. Block with used to point, which range to work with.
Notice, that it's started to y-1 row, we'll explain that later. In .Autofilter
1 is column number (relative to range), second argument is condition. So after
using autofilter we have only rows started with a. Then we apply format (notice,
that bold applied to first column and font size to all cells. When we remove
autofilter we have formatted only these rows, which we filtered.
To apply conditional format you may have one field drawn from access database,
which later will be deleted, containing FormatID. Then you filter them one by
one and apply required formats and delete the column with FormatID.
There is one unpleasant moment in this technology. When apply format to filtered
range you'll have first row formatted as well, regardless to condition. So,
then applying formats one by one you'll have first row formatted as if it has
las used FormatID. To avoid that there are several methods. For example, if
you know what is FormatID of the first row you can apply that format last. I
prefer to format range including one row higher than first (y-1 in the sample)
and delete or hide it later.
Applying of autofilter it is also possible to create outline in a fast and quite
a simple way:
We modify preceding sample:
|
|
After filtering rows we set the indent level and call the NewGroup Function.
New Group accept range as argument, the trick is that using .SpecialCells(xlCellTypeVisible)
we pass to subroutine undound range of filtered group of rows. So, in NewGroup
for each area we perform the group operation.
This row:
.Worksheet.Outline.SummaryRow = xlSummaryAbove
used to display "+" symbol on top of the group.
Below is a function, which helps you to draw hierarchical headers on given worksheet. By hierarchical header (hh) I mean that you set first row (a,b,c) then second (1,2) and have a result on a page that shows:
|
a
|
b
|
c
|
|||
|
1
|
2
|
1
|
2
|
1
|
2
|
You can specify as much "floors" as you wish, and you can set "irregular"
headers (for example in C column there may be not 2 subcolumns, but 5 with different
names. Finally, setting Palki option to true, you will have a borders drawn
in a row below the lowest flow, and borders between A, B and C will be thick
and between 1 and 2 will be thin. Later you can copy / past formats to whole
range of data and all the columns will be divided as described. That's a bit
complicated to call that function, but if you'll spend some times with example
you'll understand.
|
|
© Andrew Semenov 2003
All rights reserved