Featured Post

Excel Macros: automating the boring stuff

Work Smarter – outsource the boring tasks to Excel Excel macros are “Excel Apps”. Some smaller, some larger, but all of them are “Apps” th...

Thursday, November 24, 2016

Excel version & Row and Column limits

If macros must run on multiple versions of Excel, care is needed not to overrun the older versions limits. Microsoft made a major change to the Row and Column limits in Excel 2007:

Until Excel 2003:
  • 65,536 Rows
  • 256 Columns
From Excel 2007
  • 1,048,576 Rows
  • 16,384 Columns
There are other changes, like unlimited number of sheets and the size of content each cell can have. All huge increases from the previous versions. Excel limits full list can be found here:

https://support.office.com/en-us/article/Excel-specifications-and-limits-CA36E2DC-1F09-4620-B726-67C00B05040F

It can be easy to cross the smaller limits, specially the 256 columns. If the macro adds data in columns, and the input is large, it can easily step over 256, crashing the macro in the process.

To avoid this, macros should check the Excel version they are running and the respective limits. This can be used to check if the limit is going to be cross and show a friendly error message to the user, instead of just crashing unexpectedly.

GetExVersion() 

This function takes no parameters and just returns the Excel version running the macro. Notice that Excel versions are numbers, not the commercial naming – Excel 2007 is 12, Excel 2016 is 16.



With this function we can define 2 more to retrieve the Row and Column limits:

GetMaxRows(nVersion”) eg: GetMaxRows (12)
GetMaxColss(nVersion”) eg: GetMaxCols (12)

Both functions receive the version number as a parameter and return the respective max row or column number (note: it is not the number of rows or columns, but the number of the last one – which is 1 less, so 255 for the last of 256 columns):




The easiest way to use this functions it to call them using the GetExVersion() function as a parameter. This way, it is complete self-sufficient – no need to provide any information to get a result:

nMaxRowsVariable = GetMaxRows(GetExVersion())


You can copy the code here:

Public Function GetExVersion() As Long
' Return Excel version number
Dim nOut As Long                            ' Declare the variables

    nOut = Val(Application.Version)         ' Get Version Number from system variable
    
GetExVersion = nOut                         ' Return result to caller

End Function

Public Function GetMaxRows(nVersion As Long) As Long
' Return the maximum number of rows
Dim nOut As Long                            ' Declare the variables

    If nVersion >= 12 Then                  ' If >= Excel 2007
        nOut = (2 ^ 20) - 1                 ' New versions limit
    Else
        nOut = 65535                        ' Older versions limit
    End If
    
GetMaxRows = nOut                           ' Return result to caller

End Function

Public Function GetMaxCols(nVersion As Long) As Long
' Return the maximum number of columns
Dim nOut As Long                            ' Declare the variables

    If nVersion >= 12 Then                  ' If >= Excel 2007
        nOut = 16383                        ' New versions limit
    Else
        nOut = 255                          ' Older versions limit
    End If
    
GetMaxCols = nOut                           ' Return result to caller

End Function

Sunday, November 20, 2016

Manual Fallback: The Invisible Worker Rule

There are many ways to write macros. From experience, on a business environment, there are some general rules that will make them easier to write and use at the long term.

There is however one rule to “rule them all":

  • MANUAL FALLBACK “the invisible worker”: the best macros are “invisible”. They produce Excel sheets exactly like the ones being created manually. They do not have macros themselves or macro functions, they contain numbers, text and links. Once done, it is impossible to distinguish between the macro output or a manual created one;

This is very important as it guarantees a fallback – if for any reason the macro fails (and they do fail) and it is not possible to overcome the issue (because the person who did the macro is not available, or it will take too much time), it can still be done by hand.

As an example: on one company, every month, hundreds of similar Excel files must be linked into a “consolidated” view. All input files where pretty similar but with slight differences, so linking could not solve the problem because the exact cells to link would change every month. They had to be found by criteria, not hard links to specific cells. This was done manually by one person for a whole week, and would usually have errors and hundreds of links would be created from the consolidated file to each input file.

Instead of creating a macro on the consolidated file “pulling” the values, the macro was created on a separated file. It would scan all input files and create the links on the consolidated file. Once done, it was similar to the file that was manually created before. But it would only took 2 hours to run and had no errors.

So, the macro was an “invisible worker”, doing in 2 hours the exact same work as before in 5 days, freeing one person to do values added tasks instead on Excel links. But the end result can still be checked, validated or performed manually, in case the macro fails.

Work smarter, outsource your boring work to your “invisible worker”!