Total newbie question involving pasting someone else's Macro

T

Tibbs

I want to be able to use this macro:

Function SumByColor(InRange As Range, WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Double
'
' This function return the SUM of the values of cells in
' InRange with a background color, or if OfText is True a
' font color, equal to WhatColorIndex.
'
Dim Rng As Range
Dim OK As Boolean

Application.Volatile True
For Each Rng In InRange.Cells
If OfText = True Then
OK = (Rng.Font.ColorIndex = WhatColorIndex)
Else
OK = (Rng.Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(Rng.Value) Then
SumByColor = SumByColor + Rng.Value
End If
Next Rng

End Function

In my workbooks.

How do I do it, or where do I put it?

As you can probably tell, I have no clue how macros or VB work, jus
thought I'd jump in and ask the question....

Many thanks in advance,

Chri
 
C

Chip Pearson

Tibbs,

Use ALT+F11 to open the VBA Editor. In the editor, go to the
Insert menu and choose Module. This will open a code window on
the right side of the main editior window. Paste the code in
that window. Then, you can call the SumByColor function directly
from a worksheet cell with a formula like

=SumByColor(A1,6,False)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
N

Niek Otten

Tools>Macro>Visual Basic Editor (you can see now that you could have keyed
ALT+F11)
Insert>Module
Paste the code in the blank code window
ALT+F11 (again; it toggles between your spreadsheet and the macro code)
Now you can type the function call in a cell and see the result.

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top