Using XLM with Range Names and Conditional Formatting to automatically format spreadsheets according to cell content

AID: 7629
  • Status: Published

8160 points

  • Bybrettdj
  • TypeTips/Tricks
  • Posted on2011-09-13 at 05:05:02
Awards
  • Experts Exchange Approved

Introduction


This Article is a follow-up to my Mappit! Addin Article, it was inspired by an email posting I made to EUSPRIG,

I will briefly cover:

1) An overview of how XLM adds significant flexibility and further options to a modeller's toolkit.
2) A non-VBA technique that uses conditional formatting to immediately identify cells that are:
  • Numeric inputs (Constants)

  • Formulae

  • Formulae (resulting in text output)

  • Links to internal worksheets

  • Links to external Workbooks

3) A non-VBA approach that could be used to identify inconsistent formulae.

XLM in Excel


Excel 4 macros (XLM for short) was the initial Excel macro language, later superseded by the introduction of VBA in Excel 5.0 in 1993. Currently there is still support for legacy XLM macros in all Excel versions including Excel 2010 (Version 14), although it should be noted that there is no guarantee from Microsoft with respect to future XLM support.

While it isn't possible to use XLM directly in spreadsheet formulae, XLM can be deployed via Range Names, or by using the ExecuteExcel4Macro method in VBA. This Article makes use of the Range Name method, please see the Useful Links list at bottom for a link to John Walkenback's "A VBA Function To Get A Value From A Closed File" which is an excellent example of using ExecuteExcel4Macro.

One of the great advantages of XLM was that in prior years was that it provided the ability for advanced users to build relatively complex solutions without alarming users with the "Enable Macros" prompt  triggered by VBA. This workaround is unfortunately - or fortunately depending on your viewpoint - obsolete as Microsoft has now built in security warnings to Excel (including older versions) that detect and flag XLM.

Using Conditional Formatting with XLM Range Names to automatically highlight cell types


The Excel Experts E-Letter is a series of newsletters produced by David Hager between 1999-2001. It is an absolute treasure trove for the budding Excel power user that I highly recommend. This Article uses concepts from Issues 3 and 8 - well worth looking if you have read this far. I first looked at applying these techniques to automatically format spreadsheets back in 2002 but it was impractical at this time as Excel supported only 3 conditional formats (ignoring workarounds) until Excel 2007 (Version 12).

All the screenshots that follow apply to  Excel 2010.

Formatting Cell Content Type


The figure below shows the intended cell automatic formatting
rn1a.bmp
  • 137 KB
  • Legend
Legend


The following four Range Names underpin this formatting approach.

GlobalRef will be used to link each individual cells content to the formatting options.
GlobalCellRef makes the formula of each cell available for testing as to whether the cell may link to other closed workbooks (will contain a ".xls") or potentially other worksheets (the cell formula will contain a "!").
GlobalIsText returns True where the cell value is text
IsFormula returns True where the cell contains a formula

The full set of range names are displayed below.
rn1b.bmp
  • 914 KB
  • Range Names
Range Names


See Laurent Longre's site for A listing of the GET.CELL methods

In descending priority we can now apply the five Conditional Formatting options
  • IsBookLink =NOT(ISERROR(FIND(".xls",GlobalCellRef)))

  • IsSheetLink =NOT(ISERROR(FIND("!",GlobalCellRef)))

  • IsInput =AND(ISNUMBER(GlobalRef),NOT(IsFormula))   [ie, the entry is a numeric constant]

  • AND(IsFormula,GlobalIsText)  [ie, is the cell a formula, and is the output text]

  • IsFormula =GET.CELL(48,GlobalRef)


A conditional formatting example is shown below.
rn2.bmp
  • 415 KB
  • Conditional Formatting
Conditional Formatting


Note that I did caveat the identification of workbook links and internal worksheet links with a "may", it is possible that false workbook or internal worksheet links will be indentified, the most likely culprit being a INDIRECT formula containing a "!". If this is an issue for your workbooks then the Conditional Formatting tests can be beefed up to exclude these false flags.
For example, using IsSheetLink  =AND(NOT(ISERROR(FIND("!",GlobalCellRef))),ISERROR(FIND("INDIRECT",GlobalCellRef)))

Detecting Inconsistent Cell Formula


There are at least two VBA methods to identify inconsistent cell formulae:
1) Using the SpecialCells Row_Differences and Column Differences
2) Comparing R1C1 values using ranges or variant arrays (Mappit! approach)

The XLM/Range Name/Conditional Formatting approach provides another method to highlight inconsistent formulae but with a health warning that this method is more cumbersome than the formatting cell content type example.

At this stage I will quickly explain at a high level how Mappit! processes formulae comparisons ...... which should make it easier to understand why this approach is harder to implement with conditional formatting given each user has to deploy the same steps manually.

Mappit! uses VBA to:
1 Identifies formulae ranges (for example from A2:B6, then B10:Z10),  and then processes each formulae block,
2 Checks whether the range being examined is either at the top of the worksheet (no cells above), and/or at the far left (no cells to the left) and so on,
3 Checks whether there is a formula above and/or to the left of the formula being examined,
4 Checks whether the cell being examined is inconsistent with formulae that may exist above and to the left,
5 Distinguishes between a "starter" cell at the top left of a range and a cell that is inconsistent with a formulae above or to the immediate left.

To apply the same approach with  XLM/Range Name/Conditional Formatting Excel must be run in R1C1 notation

As per the earlier example a list of the Range Names that will be used with Conditional Formatting is shown below.
rn3.bmp
  • 995 KB
  • Range Names
Range Names


There are seven possible formula evaluations
1) The formula does not have a cell with a formula to the immediate left, or a cell with a formula immediately above - no formatting
2) The formula is to the right of a cell that contains a formula
    i) Green if consistent
   ii) Blue if inconsistent
3) The formula is below a cell that contains a formula
    i) Green if consistent
   ii) Yellow if inconsistent
4) The formula is to right of a cell that contains a formula and below a cell that contains a formula
    i) Green if consistent
   ii) Grey at the left if inconsistent with the formula to the left, but consistent with the formula in the cell above
   iii) Grey at the top if consistent with the formula to the left, but inconsistent with the formula in the cell above
   iv) Dark gray if inconsistent with both the formulae in the cells to the left and above

Formulae that occur in the top row or leftmost column are not formatted as more complicated error handling has not been added in this example to cope with these two conditions (Step 2 in Mapiit!).

The six Conditional Formatting options applied to produce the six colourings are:
  • 2(i),3(i) and 4(i)       =AND(IsFormula,OR(AND(TopOnly,TopCell=ActiveCell),AND(LeftOnly,LeftCell=ActiveCell),AND(Both,TopCell=ActiveCell,LeftCell=ActiveCell)))

  • 2(ii)               =AND(IsFormula,LeftOnly,LeftCell<>ActiveCell)

  • 3(ii)         =AND(IsFormula,TopOnly,TopCell<>ActiveCell)

  • 4(ii)        =AND(IsFormula,Formula,Both,TopCell=ActiveCell,LeftCell<>ActiveCell)

  • 4(iii)        =AND(IsFormula,Both,TopCell<>ActiveCell,LeftCell=ActiveCell)

  • 4(iv)         =AND(IsFormula,Both,TopCell<>ActiveCell,LeftCell<>ActiveCell)

Note the IsFormula test that is present in all of these formatting options, if the cell doesn't contain a formula then this test prevents a Constant cell from being incorrectly coloured.
rn4.bmp
  • 412 KB
  • Conditional Formatting
Conditional Formatting


An example screenshot of this result of applying these range names and conditional formatting to a block of cells containing formulae and text is shown below.
rn5.bmp
  • 637 KB
  • Sample Excel Formatting
Sample Excel Formatting


The Mappit! audit run on the same worksheet is shown here.
rn6.bmp
  • 196 KB
  • Mapitt! Output
Mapitt! Output


Sample ExcelFiles
CellsR1C1.xlsm
  • 10 KB
  • Inconsistent Excel Sample
CellsR1C1.xlsm
IsFormula.xlsm
  • 11 KB
  • Cell Format Sample
IsFormula.xlsm

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If you found the article helpful please click on the "YES" button after the question below.
This utility has been helpful to me and I hope you find an opportunity to use it as well.

If you didn't or otherwise want to vote No, please leave a comment and give me an
opportunity to respond. It will give me a chance to improve this article and others I
would like to write.

If you liked this article and want to see more from this author, please click here.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Useful Links


Expert Excel E-letter
A listing of the GET.CELL methods
Migrating Excel 4 Macros to VBA
XL4 Macro Functions In Names
A VBA Function To Get A Value
    Asked On
    2011-09-13 at 05:05:02ID7629
    Tags

    audit

    ,

    XLM

    ,

    EUSPRIG

    ,

    inconsistent formula

    ,

    error

    Topic

    Microsoft Excel Spreadsheet Software

    Views
    2203

    Comments

    Expert Comment

    by: dlmille on 2012-01-10 at 18:05:41ID: 34453

    I've been dabbling in this ever since your article was posted.  I can see the possibilities.

    Voted "YES" above.

    Dave

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top MS Excel Experts

    1. dlmille

      1,351,499

      Genius

      10,680 points yesterday

      Profile
      Rank: Genius
    2. ssaqibh

      542,555

      Sage

      0 points yesterday

      Profile
      Rank: Genius
    3. rorya

      381,757

      Wizard

      4,225 points yesterday

      Profile
      Rank: Savant
    4. imnorie

      334,112

      Wizard

      0 points yesterday

      Profile
      Rank: Genius
    5. teylyn

      282,850

      Guru

      20 points yesterday

      Profile
      Rank: Genius
    6. barryhoudini

      280,460

      Guru

      0 points yesterday

      Profile
      Rank: Genius
    7. redmondb

      235,511

      Guru

      2,000 points yesterday

      Profile
      Rank: Sage
    8. matthewspatrick

      230,947

      Guru

      2,010 points yesterday

      Profile
      Rank: Savant
    9. byundt

      197,840

      Guru

      820 points yesterday

      Profile
      Rank: Savant
    10. zorvek

      144,626

      Master

      0 points yesterday

      Profile
      Rank: Savant
    11. StephenJR

      136,537

      Master

      0 points yesterday

      Profile
      Rank: Genius
    12. nutsch

      117,005

      Master

      0 points yesterday

      Profile
      Rank: Genius
    13. gowflow

      110,036

      Master

      0 points yesterday

      Profile
      Rank: Sage
    14. MartinLiss

      107,333

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    15. GlennLRay

      95,652

      Master

      0 points yesterday

      Profile
      Rank: Guru
    16. robhenson

      90,250

      Master

      0 points yesterday

      Profile
      Rank: Sage
    17. ScriptAddict

      88,470

      Master

      0 points yesterday

      Profile
      Rank: Guru
    18. kgerb

      85,022

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    19. aikimark

      84,456

      Master

      3,310 points yesterday

      Profile
      Rank: Genius
    20. andrewssd3

      80,242

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    21. Wiesje

      69,918

      Master

      0 points yesterday

      Profile
      Rank: Master
    22. Shanan212

      66,418

      Master

      0 points yesterday

      Profile
      Rank: Master
    23. krishnakrkc

      59,548

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    24. Michael74

      54,744

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    25. regmigrant

      51,070

      Master

      0 points yesterday

      Profile
      Rank: Guru

    Hall Of Fame