Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Shading Rows

I would like a row shaded based upon one of two values in column B. The
number of rows will change from worksheet to worksheet and my preference is
to use VBA as opposed to conditional formatting. Any suggestions?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Shading Rows

Dim lastrow as Long, i as Long
lastrow = cells(rows.count,1).End(xlup)
for i = 1 to lastrow
if cells(i,"B").Value < 3 then
rows(i).Interior.ColorIndex = 3
elseif cells(i,"B").Value 10 then
rows(i).Interior.ColorIndex = 5
else
rows(i).Interior.ColorIndex = xlNone
end if
Next

--
Regards,
Tom Ogilvy

"briank" wrote in message
...
I would like a row shaded based upon one of two values in column B. The
number of rows will change from worksheet to worksheet and my preference

is
to use VBA as opposed to conditional formatting. Any suggestions?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Shading Rows

Thanks Tom. Your code works great.
Regards, Brian

"Tom Ogilvy" wrote:

Dim lastrow as Long, i as Long
lastrow = cells(rows.count,1).End(xlup)
for i = 1 to lastrow
if cells(i,"B").Value < 3 then
rows(i).Interior.ColorIndex = 3
elseif cells(i,"B").Value 10 then
rows(i).Interior.ColorIndex = 5
else
rows(i).Interior.ColorIndex = xlNone
end if
Next

--
Regards,
Tom Ogilvy

"briank" wrote in message
...
I would like a row shaded based upon one of two values in column B. The
number of rows will change from worksheet to worksheet and my preference

is
to use VBA as opposed to conditional formatting. Any suggestions?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Shading Rows


Hi Tom,

I've tried to use your code (modified for my sheet) and I've become
stuck:

Sub highlightrow()

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp)
For i = 1 To lastrow
If Cells(i, "F").Value AND(=5,=<7) Then
Rows(i).Interior.ColorIndex = 3
ElseIf Cells(i, "F").Value 10 Then
Rows(i).Interior.ColorIndex = 5
Else
Rows(i).Interior.ColorIndex = xlNone
End If
Next

End Sub

This gets a 'Compile error; Expect: expression)

which leads to

-Expected: Expression. For example, when pasting code from the Object
Browser, you may have forgotten to specify a value for a named
argument.
Workbook.Add (X:= ) ' Error because no value assigned to
' named argument.

Either add a value for the argument, or delete the argument if it's
optional. -

I thought it would be just a matter of changing th <3 to AND(=5,=<7)
to make the condition between 2 values but obviously that's not the
case here.

I would be grateful if you could you show me where I've gone wrong and
why please.

Thank you


--
Daminc
------------------------------------------------------------------------
Daminc's Profile: http://www.excelforum.com/member.php...o&userid=27074
View this thread: http://www.excelforum.com/showthread...hreadid=465603

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Shading Rows


I've managed to get rid of the red text by converting the code to:

Sub highlightrow()

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp)
For i = 1 To lastrow
If Cells(i, "F").Value(i, "F" = 5 And i, "F" <= 7) Then
Rows(i).Interior.ColorIndex = 3
ElseIf Cells(i, "F").Value(i, "F" 7) Then
Rows(i).Interior.ColorIndex = 5
Else
Rows(i).Interior.ColorIndex = xlNone
End If
Next

End Sub

but now I get a Type Mismatch (Error 13) for line
lastrow = Cells(Rows.Count, 1).End(xlUp)

Any ideas

--
Damin
-----------------------------------------------------------------------
Daminc's Profile: http://www.excelforum.com/member.php...fo&userid=2707
View this thread: http://www.excelforum.com/showthread.php?threadid=46560



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Shading Rows

Maybe you want something like:

Option Explicit

Sub highlightrow()

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastrow
If (Cells(i, "F").Value = 5 _
And Cells(i, "F").Value <= 7) Then
Rows(i).Interior.ColorIndex = 3
ElseIf Cells(i, "F").Value 10 Then
Rows(i).Interior.ColorIndex = 5
Else
Rows(i).Interior.ColorIndex = xlNone
End If
Next i

End Sub




Daminc wrote:

I've managed to get rid of the red text by converting the code to:

Sub highlightrow()

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp)
For i = 1 To lastrow
If Cells(i, "F").Value(i, "F" = 5 And i, "F" <= 7) Then
Rows(i).Interior.ColorIndex = 3
ElseIf Cells(i, "F").Value(i, "F" 7) Then
Rows(i).Interior.ColorIndex = 5
Else
Rows(i).Interior.ColorIndex = xlNone
End If
Next

End Sub

but now I get a Type Mismatch (Error 13) for line
lastrow = Cells(Rows.Count, 1).End(xlUp)

Any ideas?

--
Daminc
------------------------------------------------------------------------
Daminc's Profile: http://www.excelforum.com/member.php...o&userid=27074
View this thread: http://www.excelforum.com/showthread...hreadid=465603


--

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
Shading rows MichaelZ Excel Worksheet Functions 2 March 28th 08 07:08 PM
Shading of rows STS Excel Worksheet Functions 8 March 18th 08 07:22 PM
shading rows peter mizzi Excel Discussion (Misc queries) 3 May 27th 07 03:09 PM
Repeated shading of rows?? Andy Excel Discussion (Misc queries) 8 September 24th 06 10:28 AM
shading different rows when a value changes in col. Linc Excel Worksheet Functions 19 August 4th 05 12:54 AM


All times are GMT +1. The time now is 02:45 PM.

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"