Google Answers Logo
View Question
 
Q: Excel formulas based on cell formatting ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: Excel formulas based on cell formatting
Category: Computers > Software
Asked by: tiger72-ga
List Price: $10.00
Posted: 02 Jan 2003 11:09 PST
Expires: 01 Feb 2003 11:09 PST
Question ID: 136528
In Excel 97 or 2000 how can I create a formula where the
result is conditioned on the formatting of certain cells?  For
example, I have a column of numbers, some of which are bolded, and I
want to be able to add all the bolded cells, but not the unbolded
cells.
Answer  
Subject: Re: Excel formulas based on cell formatting
Answered By: mrbuzz-ga on 02 Jan 2003 12:45 PST
Rated:5 out of 5 stars
 
Hi tiger72-ga,

I've searched through the Excel help files and was unable to find a
function that would recognize bolding.  The closest function was
Cell() although that only worked with cell formatting (General,
Number, etc).

The solution I've come up with is based in Excel VBA and is quite
simple and does exactly what you've asked for.  Please follow the
steps below to add the User Defined Function to Excel:

1. In your Excel worksheet window, press Alt+F11.
2. In the new Microsoft Visual Basic window, click on the Insert menu,
Module.
3. In the right hand pane, in the window titled "Module1 (Code)",
paste in the following function:

Function SumBold(CellRange As Range)
    Dim sumb
    For Each cell In CellRange.Cells
        If cell.Font.Bold = True Then
            sumb = sumb + cell.Value
        End If
    Next
    SumBold = sumb
End Function

4. Press Alt+Q to save.

Now in any cell in this workbook, you can enter the formula =SumBold()
to have it add up all bolded numbers within a range.  You would use
=SumBold() in the exact way that you would use =SUM().

One caveat is that Excel does not appear to recognize bolding as an
event, so when you bold/unbold new items, the sum may not reflect the
new changes.  You'll have to use Ctrl+Alt+F9 to have it recalculate
all formulas on the sheet.

If you wish to use this new SumBold() formula in another workbook,
simply repeat the instructions above to transfer the SumBold()
formula.

Hope this helps,
mrbuzz-ga


Additional Information:

Excel User Define Function VBA. Custom Functions - User Defined
Functions
http://www.ozgrid.com/VBA/Functions.htm

Excel Pages - Calculation Secrets
http://www.decisionmodels.com/calcsecretsh.htm


Search Terms Used:
://www.google.com/search?q=excel+vba+recalculate
://www.google.com/search?q=excel+vba+custom+functions
://www.google.com/search?q=excel+vba+bold+format+function

Request for Answer Clarification by tiger72-ga on 02 Jan 2003 13:59 PST
Thanks, mrbuzz, you make it sound easy.  Would a similar approach work with italics?

Clarification of Answer by mrbuzz-ga on 02 Jan 2003 14:58 PST
Hi tiger72-ga,

I'm glad you like the approach. :)

Yes, for italics it's virtually the same code.  Simply change the
following line from:
If cell.Font.Bold = True Then

And change it to:
If cell.Font.Italic = True Then

You'll also have to change the name of the function.  So for a Sum of
Italics, the code would look like:

Function SumItalics(CellRange As Range) 
    Dim sumi
    For Each cell In CellRange.Cells 
        If cell.Font.Italic = True Then 
            sumi = sumi + cell.Value 
        End If 
    Next 
    SumItalics = sumi
End Function


If you're the adventurous type, you can check out what other
formatting can be detected through Excel VBA pressing F2 in the
Microsoft Visual Basic window, to bring up the Object Browser.  Then
in the first dropdown box that might say "<All Libraries>", choose
Excel.  Then in the left pane, scroll down to Font.  The right pane
now lists the complete set of properties that can be used in a user
defined function.

Good luck,
mrbuzz-ga
tiger72-ga rated this answer:5 out of 5 stars

Comments  
Subject: Re: Excel formulas based on cell formatting
From: tar_heel_v-ga on 02 Jan 2003 11:37 PST
 
You may want to give Spreadsheet Assistant a try as it appears you can
select cells based upon format.  You can see and download a free trial
at http://www.office-addins.com/excel-add-ins/Spreadsheet-Assistant-2.html

-THV
Subject: Re: Excel formulas based on cell formatting
From: tar_heel_v-ga on 02 Jan 2003 12:55 PST
 
Nicely done, mrbuzz!

-THV
Subject: Re: Excel formulas based on cell formatting
From: mrbuzz-ga on 02 Jan 2003 15:01 PST
 
Hehe, thanks tar_heel_v!  I've used VBA in other Office applications
before but it was my first time using it in Excel.  I learned quite a
bit myself.  :)

mrbuzz-ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy