Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Using VBA to evaluate ranges of data

I have written some VBA which looks at 2 specific cells whenever another cell
is changed and changes the color of the target cell based on whether the
target value is between the 2 other cells (numbers). Although it works
perfectly, I am looking to compact the code. Right now I have a block of
code for each row in a large range (about 100 rows). The 2 numbers which
determine the high and low will always be in the same columns and in the same
row as the changed cell. Below is a sample of my code. I thought about
using a For Each statement but I'm not sure how I need to write it. Any
ideas?

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("C14:N14")) Is Nothing Then
If Target <= Range("P14").Value And Target = Range("Q14").Value Or Target
= "" Then
Target.Font.Color = RGB(0, 0, 0)
Else: Target.Font.Color = RGB(255, 0, 0)
End If
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Using VBA to evaluate ranges of data

Instead of:
Range("P14")
Use:
Cells(Target.row, "P")
Do the same for the column Q cell.
HTH Otto

"josh" wrote in message
...
I have written some VBA which looks at 2 specific cells whenever another
cell
is changed and changes the color of the target cell based on whether the
target value is between the 2 other cells (numbers). Although it works
perfectly, I am looking to compact the code. Right now I have a block of
code for each row in a large range (about 100 rows). The 2 numbers which
determine the high and low will always be in the same columns and in the
same
row as the changed cell. Below is a sample of my code. I thought about
using a For Each statement but I'm not sure how I need to write it. Any
ideas?

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("C14:N14")) Is Nothing Then
If Target <= Range("P14").Value And Target = Range("Q14").Value Or Target
= "" Then
Target.Font.Color = RGB(0, 0, 0)
Else: Target.Font.Color = RGB(255, 0, 0)
End If


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Using VBA to evaluate ranges of data

Thanks for the responses. Both were helpful.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Using VBA to evaluate ranges of data

row 140 is the last row of your worksheet


Private Sub Worksheet_Change(ByVal Target As Range)

if Target.row = 14 and Target.row <= 150 and _
target.Column = 3 and Target.Column <= 14 then

If (Target <= Range("P" & target.row).Value And _
Target = Range("Q" & target.row).Value) Or _
Target = "" Then

Target.Font.Color = RGB(0, 0, 0)
Else
Target.Font.Color = RGB(255, 0, 0)
End if
end if


"josh" wrote:

I have written some VBA which looks at 2 specific cells whenever another cell
is changed and changes the color of the target cell based on whether the
target value is between the 2 other cells (numbers). Although it works
perfectly, I am looking to compact the code. Right now I have a block of
code for each row in a large range (about 100 rows). The 2 numbers which
determine the high and low will always be in the same columns and in the same
row as the changed cell. Below is a sample of my code. I thought about
using a For Each statement but I'm not sure how I need to write it. Any
ideas?

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("C14:N14")) Is Nothing Then
If Target <= Range("P14").Value And Target = Range("Q14").Value Or Target
= "" Then
Target.Font.Color = RGB(0, 0, 0)
Else: Target.Font.Color = RGB(255, 0, 0)
End If

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
Evaluate Formula with Named Ranges does not work if Workbook isalready open todtown Excel Programming 1 November 15th 07 07:33 AM
Evaluate Formula with Named Ranges does not work if Workbook isalready open todtown Excel Programming 0 November 15th 07 04:51 AM
Evaluate Formula with Named Ranges does not work if Workbook isalready open todtown Excel Programming 0 November 15th 07 04:51 AM
Why won't Sumproduct funciton evaluate this data [email protected] Excel Worksheet Functions 1 March 8th 06 07:56 PM
Need a macro to evaluate and sift-through data Ronny Hamida[_2_] Excel Programming 2 May 25th 04 09:13 PM


All times are GMT +1. The time now is 09:46 PM.

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

About Us

"It's about Microsoft Excel"