Solved

Embedding formula in VBA

Posted on 2014-03-16
13
145 Views
Last Modified: 2014-03-25
Private Sub cmdResultsShadedCells_Click()
ActiveSheet.Range("A3,A7,A8").Select
Selection.Interior.Color = RGB(255, 0, 0)
Range("A9").Select
'Here I would like to sum only those cells colored red;
Range("A1").Select
End Sub

Open in new window


I'm trying to sum all cells colored red in my code above.
0
Comment
Question by:Frank Freese
  • 7
  • 5
13 Comments
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 150 total points
ID: 39933166
You should use Select as little as possible so I combined your lines 2 and 3
Private Sub cmdResultsShadedCells_Click()
Dim cel As Range
Dim dblTot As Double
ActiveSheet.Range("A3,A7,A8").Interior.Color = RGB(255, 0, 0)
Range("A9").Select
'Here I would like to sum only those cells colored red;
For Each cel In ActiveSheet.UsedRange
    If cel.Interior.Color = RGB(255, 0, 0) Then
        dblTot = dblTot + cel.Value
    End If
Next
Range("A1").Select
End Sub

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 39933184
Martin,
Welcome back from you cruise in January. I'm getting close to finishing my project.
The answer I get for dblTotal is what I am expecting. The error I'm receiving is "Type mismatch"
Since dblTotal is defined as double, what formatting for the cell should I use. I think that is why I'm getting this error?
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39933210
Try changing line 9 to

dblTot = dblTot + CDbl(cel.Value)
0
 

Author Comment

by:Frank Freese
ID: 39933216
same problem
0
 

Author Comment

by:Frank Freese
ID: 39933230
I meant to mention that the answer appears in A9
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39933295
Can you attach your workbook, or at least one that demonstrates the problem?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 350 total points
ID: 39933788
Hi,

Maybe one of the values is not numeric

Private Sub cmdResultsShadedCells_Click()
Dim cel As Range
Dim dblTot As Double
ActiveSheet.Range("A3,A7,A8").Interior.Color = RGB(255, 0, 0)
'Here I would like to sum only those cells colored red;
For Each cel In ActiveSheet.UsedRange
    If IsNumeric(cel.Value) And cel.Interior.Color = RGB(255, 0, 0) Then
        dblTot = dblTot + cel.Value
    End If
Next
Range("A9").Value = dblTot
Range("A1").Select
End Sub

Open in new window

but maybe you only want to sum in A9 : A3,A7 and A8
you could use
Range("A9").Formula = "=SUM(A3,A7,A8)"

Open in new window

or if you do not want a formula
Range("A9").Value= Evaluate("SUM(A3,A7,A8)")

Open in new window

Regards
0
 

Author Comment

by:Frank Freese
ID: 39934415
Thanks for all the help. Last night I read about the .Value and .Formula for the Range and being late as it was thought to try it this morning.  Tried the .Formula first and problem solved.
Then I read these responses and it felt good to know that I was moving in the right direction.
Thanks and I'll share the points here between the two of you. The continual effort of all has value to me.
0
 

Author Closing Comment

by:Frank Freese
ID: 39934417
Thank you very much
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39934493
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2013
0
 

Author Comment

by:Frank Freese
ID: 39953815
Martin,
I dislike asking this of you. I need to post a question on some work you did on that big workbook last fall. It has to deal with when I select a category then list the topics under that category. Your code works fine on the first line item but not on any other additions to topics when I select a different category. The information is being pulled from the wrong column and I don't see the problem. This is a new workbook since I completed the big one several weeks ago.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39953826
I'll be happy to help. Post a new question and I (or someone) will give it a shot.
0
 

Author Comment

by:Frank Freese
ID: 39953867
Give me a few minutes - maybe, just maybe I found the problem
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
macro for active word document 4 39
Search for a value in Column? 5 21
Excel for Mac - How make those Tabs larger? 2 31
Split a string in excel 9 30
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now