VBA Code Help

jhedges

Board Regular
Joined
May 27, 2009
Messages
208
I have an Excel 07 spreadsheet that is A1:M2, setup as a table (Row 1 headers). The spreadsheet will grow as data is added. I have the following code that I have found on the message board. Column A has a formula that will display the cases indicated in the code below. The formula is working correctly to display the text; however, the cell color code is not working. The calendar code is working correctly. No error message, just not coloring the cells.

Code:
Option Compare Text 'A=a, B=b, ... Z=z
Option Explicit
 
Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim Cell As Range
Dim Rng1 As Range
 
    On Error Resume Next
    Set Rng1 = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas, 1)
    On Error GoTo 0
    If Rng1 Is Nothing Then
        Set Rng1 = Range(Target.Address)
        Else
        Set Rng1 = Union(Range(Target.Address), Rng1)
    End If
    For Each Cell In Rng1
        Select Case Cell.Value
            Case vbNullString
                Cell.Interior.ColorIndex = xlNone
                Cell.Font.ColorIndex = xlNone
                Cell.Font.Bold = False
            Case "ACTIVE"
                Cell.Interior.ColorIndex = 4
                Cell.Font.ColorIndex = 2
                Cell.Font.Bold = True
            Case "EXPIRED"
                Cell.Interior.ColorIndex = 1
                Cell.Font.ColorIndex = 2
                Cell.Font.Bold = True
            Case "SESSION 1 PAST DUE", "SESSION 2 PAST DUE", "SESSION 3 PAST DUE", "SESSION 4 PAST DUE"
                Cell.Interior.ColorIndex = 3
                Cell.Font.ColorIndex = 2
                Cell.Font.Bold = True
            Case Else
                Cell.Interior.ColorIndex = xlNone
                Cell.Font.ColorIndex = xlNone
                Cell.Font.Bold = False
        End Select
    Next
End Sub
Private Sub Calendar1_Click()
    ActiveCell.Value = CDbl(Calendar1.Value)
    ActiveCell.NumberFormat = "mm/dd/yyyy"
    ActiveCell.Select
    Calendar1.Visible = False
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("F2:F200,J2:J200,K2:K200,L2:L200,M2:M200"), Target) Is Nothing Then
        Calendar1.Left = Target.Left + Target.Width - Calendar1.Width
        Calendar1.Top = Target.Top + Target.Height
        Calendar1.Visible = True
        ' select Today's date in the Calendar
        Calendar1.Value = Date
    ElseIf Calendar1.Visible Then Calendar1.Visible = False
    End If
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Can you give some sample data? I'm trying to test it but don't know what exactly I should be seeing.
 
Upvote 0
Alright - the code is sort of working. It is not updating or checking data that is already there, will color cells that are added automatically via the table feature. One last problem it seems to be coloring everything Red interior and White font.

Fall Into Fitness

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Calibri,Arial; FONT-SIZE: 11pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 122px"><COL style="WIDTH: 129px"><COL style="WIDTH: 142px"><COL style="WIDTH: 105px"><COL style="WIDTH: 71px"><COL style="WIDTH: 82px"><COL style="WIDTH: 81px"><COL style="WIDTH: 99px"><COL style="WIDTH: 77px"><COL style="WIDTH: 83px"><COL style="WIDTH: 89px"><COL style="WIDTH: 89px"><COL style="WIDTH: 92px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt; FONT-WEIGHT: bold"><TD> </TD><TD>A</TD><TD>B</TD><TD>C</TD><TD>D</TD><TD>E</TD><TD>F</TD><TD>G</TD><TD>H</TD><TD>I</TD><TD>J</TD><TD>K</TD><TD>L</TD><TD>M</TD></TR><TR style="HEIGHT: 32px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Status</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">First Name</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Last Name</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Personnel #</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Site</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Join Date</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">EXP. Date</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Payroll Submitted</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Trainer</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Session 1 Date</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Session 2 Date</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Session 3 Date</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Frutiger 45 Light; FONT-SIZE: 10pt; FONT-WEIGHT: bold">Session 4 Date</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD style="BACKGROUND-COLOR: #c0c0c0">New Member</TD><TD>Susan</TD><TD>Bremer</TD><TD> </TD><TD>MBC</TD><TD style="TEXT-ALIGN: right">09/26/2011</TD><TD style="TEXT-ALIGN: right">10/26/2011</TD><TD> </TD><TD>Katie</TD><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD style="BACKGROUND-COLOR: #ff0000; COLOR: #ffffff; FONT-WEIGHT: bold">New Member</TD><TD>Romain</TD><TD>McGhee</TD><TD> </TD><TD>MBC</TD><TD style="TEXT-ALIGN: right">09/26/2011</TD><TD style="TEXT-ALIGN: right">10/26/2011</TD><TD> </TD><TD>Jason</TD><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR></TBODY></TABLE>
<TABLE style="BORDER-BOTTOM-STYLE: groove; BORDER-BOTTOM-COLOR: #00ff00; BORDER-RIGHT-STYLE: groove; BACKGROUND-COLOR: #fffcf9; BORDER-TOP-COLOR: #00ff00; FONT-FAMILY: Arial; BORDER-TOP-STYLE: groove; COLOR: #000000; BORDER-RIGHT-COLOR: #00ff00; FONT-SIZE: 10pt; BORDER-LEFT-STYLE: groove; BORDER-LEFT-COLOR: #00ff00"><TBODY><TR><TD>Spreadsheet Formulas</TD></TR><TR><TD><TABLE style="FONT-FAMILY: Arial; FONT-SIZE: 9pt" border=1 cellSpacing=0 cellPadding=2><TBODY><TR style="BACKGROUND-COLOR: #cacaca; FONT-SIZE: 10pt"><TD>Cell</TD><TD>Formula</TD></TR><TR><TD>A2</TD><TD>=IF((AND(G2>0,G2<TODAY(),H2="")),"EXPIRED",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F2),ISBLANK(J2)),"Session 1 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F2)+1,ISBLANK(K2)),"Session 2 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F2)+2,ISBLANK(L2)),"Session 3 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F2)+3,ISBLANK(M2)),"Session 4 Past Due",IF((AND(G2>TODAY(),H2="")),"New Member",""))))))</TD></TR><TR><TD>G2</TD><TD>=IF(ISBLANK(F2),"",F2+30)</TD></TR><TR><TD>A3</TD><TD>=IF((AND(G3>0,G3<TODAY(),H3="")),"EXPIRED",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F3),ISBLANK(J3)),"Session 1 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F3)+1,ISBLANK(K3)),"Session 2 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F3)+2,ISBLANK(L3)),"Session 3 Past Due",IF(AND(WEEKNUM(TODAY())>WEEKNUM(F3)+3,ISBLANK(#REF!)),"Session 4 Past Due",IF((AND(G3>TODAY(),H3="")),"New Member",""))))))</TD></TR><TR><TD>G3</TD><TD>=IF(ISBLANK(F3),"",F3+30)</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>

Excel tables to the web >> http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4
 
Upvote 0
I noticed in running your code that it only seems to run on the cell that changes, and all cells with formulas which i think is your intent.

Try making the line of code:
Code:
Set Rng1 = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas, 1)

to this:
Code:
Set Rng1 = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas)

Does this get your desired results?
 
Upvote 0
Thanks for the data, let me see what i can do. i was using more simplifeied data and got it to work, but I'm seeing your same issue now....
 
Upvote 0
Actually, if you do what I said to the code, I get it to work.

I had to remove the G2() part of your first AND condition in the formula as it gave me a #REF!, I wasn;t sure what its designed to do and haven't seen that function before.

FYI, your code has an entry for a Case "ACTIVE" but your formula never evaluates to that, so that will never happen.
 
Upvote 0
Okay - I figured out all of the issues. Thanks for the help. I have one more question. If I have this workbook in a shared folder on our server and all the users pointing to it via a shortcut on their desktops will the VBA script show up with each individual user?
 
Upvote 0
I'm not sure what you mean by "will the VBA code show up with all of the users". The code is in the file and will run when being used by any/all users of the file, but unless the users open up the VB Editor they'll never see it.

Some people might have to set their security lower to open macro-enabled files, but that would be the rare person who has their security set at "High" in Excel...

HTH
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top