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
 
@footoo why I insert or delete the row I don't need that it just add the data inside cells

if you mean insert and delete and write the word paid I tried and works , there is no problem
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
@footoo why I insert or delete the row I don't need that it just add the data inside cells

if you mean insert and delete and write the word paid I tried and works , there is no problem
My comment may be relevant to other readers.
 
Upvote 0
@abdelfattah
With the code from Mohadin, if more that one cell is changed & at least one of those cells is in col E, then the code will crash.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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