VBA format a cell based on range contents

15 pts.
Tags:
*RANGE
Conditional formatting
Excel VBA
Microsoft Excel
VBA
Visual Basic for Applications
I need to create a macro that goes in to a worksheet and looks at the contents of a range to see if it contains any number other than zero. If it does, I need it to turn a separate cell yellow. 'assign variables Dim rngSat As Range Dim rngSun As Range Dim wsOT As Worksheet Set wsOT = _ Application.Workbooks("Weekend OT.xls").Worksheets("7-8") Set rngSat = _ Application.Workbooks("Weekend OT.xls").Worksheets("7-8").Range("G37:G40") Set rngSun = _ Application.Workbooks("Weekend OT.xls").Worksheets("7-8").Range("J37:J40") If rngSat.Value <> 0 Then wsOT.Cells("Y5:Y6").Interior.Color = vbYellow End If If rngSun.Value <> 0 Then wsOT.Cells("Y13:Y14").Interior.Color = vbYellow End If
ASKED: May 14, 2009  5:11 AM
UPDATED: May 20, 2009  6:04 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

If I understood correctly you want to format cells yellow where number is zero.
try the code below

Sub ChangeFormat()
Dim c As Range

For Each c In Selection
MsgBox Len(c)
If c.Value = 0 And Len(c) > 0 Then
c.Interior.Color = vbYellow
End If

Next c
End Sub

Regards,
Sarfaraz Ahmed
<a href=”http://findsarfaraz.blogspot.com”>MS Excel help</a>

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • VbaNewb13
    Here is what I ended up using 'See if anyone is working Saturday 2nd Shift, if they are shade support cells yellow If Application.WorksheetFunction.Sum(rngSat) <> 0 Then wsOT.Range("Y5:Y6,Y20,Y26,Y32,AA17,AA21:AA22,AA25,Y45:AA45").Interior.Color = vbYellow Else wsOT.Range("Y5:Y6,Y20,Y26,Y32,AA17,AA21:AA22,AA25,Y45:AA45").Interior.Color = vbWhite End If 'See if anyone is working Sunday 2nd Shift, if they are shade support cells yellow If Application.WorksheetFunction.Sum(rngSun) <> 0 Then wsOT.Range("Y13:Y14,Y22,Y28,Y34,Y48:AA48").Interior.Color = vbYellow Else wsOT.Range("Y13:Y14,Y22,Y28,Y34,Y48:AA48").Interior.Color = vbWhite End If 'Change background color to white if info has been provided For Each Cell In rngComp If Cell.Text = "" Then Cell.Interior.Color = vbYellow ElseIf Cell.Text = "TBD" Then Cell.Interior.Color = vbYellow Else Cell.Interior.Color = vbWhite End If Next Cell
    15 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following