Solved

Embedding formula in VBA

Posted on 2014-03-16
13
144 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 45

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 45

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 45

Expert Comment

by:Martin Liss
ID: 39933295
Can you attach your workbook, or at least one that demonstrates the problem?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

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 45

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 45

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

12 Experts available now in Live!

Get 1:1 Help Now