what's wrong with this code?

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i try writing code in earlier time it was working but now it works only to specific row 2,5 my idea when the value "paid" is existed in column a then should highlight the cells in columns I,J,K
as in my picture in row6 should be color because column e contains "paid"


this is my code
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("e:e")) Is Nothing Then
Dim i As Long, r1 As Range, r2 As Range
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
   For i = 2 To lr
      Set r1 = Range("e" & i)
      Set r2 = Range("i" & i & ":k" & i)
      If r1.Value = "paid" Then
      r2.Interior.color = vbBlue
      Else
      r2.Interior.ColorIndex = xlNone
      End If
   Next i
   End If
End Sub
color (1).xlsm
EFGHIJK
1casedebitcreditbalance
2paid1000200800
3unpaid10001000
4paid20005001500
5unpaid10201020
6paid12002001000
13
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E4Cell Value=1textNO
C1:E1Cell Value=1textNO
C2:C3,E6,E2:E3Cell Value=1textNO
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What row does your data in column A go down to?
 
Upvote 0
Hi
Try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("e:e")) Is Nothing Then
        If Target.Value = "paid" Then
            Target.Offset(, 4).Resize(, 3).Interior.Color = vbBlue
        Else
            Target.Offset(, 4).Resize(, 3).Interior.ColorIndex = xlNone
        End If
    End If
End Sub
 
Upvote 0
mohadin
Your code does not provide for the possibility of the target being more than one cell.
 
Upvote 0
Your code is based on the last used row in column A change the 1 below to a 5 so it is based on column E.
Rich (BB code):
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
 
Upvote 0
Solution

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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