Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Andrew Clark
 
Posts: n/a
Default Change background color

Hello,

I would like to write a function to change the background color of a
range based on the date in the first column. I am not very familiar with
VBA's objects though. This is what I had:

Private Sub Worksheet_Activate()
Dim y As Integer, strRange As String
' Ideally, I would like to go to the last row, but 200 will be
' sufficient for my purposes
For y = 0 To 200
If Month(Now) = Month(Worksheets(1).Cells(y, 0)) And _
Year(Now) = Year(Worksheets(1).Cells(y, 0)) Then
' Highlight this row (columns A to L)
strRange = "A" & y & ":L" & y
Me.Range(strRange).Select ' Just select for now
Exit Sub ' At most 1 row will be selected so I can exit now
End If
Next
End Sub

I'm sure I have nothing right since it does not work! Could someone guide
me in the right direction?

Thanks,
Andrew

  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

Have you thought of using Format|Conditional formatting?

You get up to 3 formats (plus the normal one)?

It might be simpler and less work for excel to do.


Andrew Clark wrote:

Hello,

I would like to write a function to change the background color of a
range based on the date in the first column. I am not very familiar with
VBA's objects though. This is what I had:

Private Sub Worksheet_Activate()
Dim y As Integer, strRange As String
' Ideally, I would like to go to the last row, but 200 will be
' sufficient for my purposes
For y = 0 To 200
If Month(Now) = Month(Worksheets(1).Cells(y, 0)) And _
Year(Now) = Year(Worksheets(1).Cells(y, 0)) Then
' Highlight this row (columns A to L)
strRange = "A" & y & ":L" & y
Me.Range(strRange).Select ' Just select for now
Exit Sub ' At most 1 row will be selected so I can exit now
End If
Next
End Sub

I'm sure I have nothing right since it does not work! Could someone guide
me in the right direction?

Thanks,
Andrew


--

Dave Peterson
  #3   Report Post  
Andrew Clark
 
Posts: n/a
Default

Dave Peterson wrote in
:

Have you thought of using Format|Conditional formatting?

You get up to 3 formats (plus the normal one)?

It might be simpler and less work for excel to do.


Andrew Clark wrote:


I would, but I want to highlight the entire row, not just that cell.
  #4   Report Post  
Dave Peterson
 
Posts: n/a
Default

Ah, you can get the whole row (or parts of the row to highlight):

Select say A2:L99 (with A2 the active cell).

Then format|Conditional formatting.
Use a formula that refers to the cell in column A of that row:

=TEXT($A2,"yyyymm")=TEXT($A$1,"yyyymm")

This will use the date value in A1 and compare it to the date in A2:A99. If the
year and month match, then A:L (of that row) will be formatted.

(I was confused over what cell you were using to compare all dates to.)


Andrew Clark wrote:

Dave Peterson wrote in
:

Have you thought of using Format|Conditional formatting?

You get up to 3 formats (plus the normal one)?

It might be simpler and less work for excel to do.


Andrew Clark wrote:


I would, but I want to highlight the entire row, not just that cell.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I change the background color of a cell in microsoft excel. amac Excel Discussion (Misc queries) 1 February 25th 05 07:49 PM
How do I get the font color to change automatically depending on gtcarlsbad Excel Discussion (Misc queries) 2 February 1st 05 02:39 AM
Change cell back color on click Dave Peterson Excel Discussion (Misc queries) 0 January 24th 05 10:50 PM
automatic color change in cells using a drop down list kennethwt Excel Worksheet Functions 1 January 21st 05 06:37 PM
Change Color on an IF statement craigwojo Excel Worksheet Functions 1 November 8th 04 12:14 AM


All times are GMT +1. The time now is 08:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"