Need Help in This VBA

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
i have applied this below vba to highlight cell color based on cell value (Pending) it's working when i am typing Pending in cell value of Column C Range & here i have created a drop down list with text Pending in Column C Range & when i selected Pending from drop down then the cell is not highlighting

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
    With Sheet1
    For Each rCell In Range("C1:C100")
        If rCell.Value = "Pending" Then
            rCell.Interior.Color = vbRed
        Else
            rCell.Interior.Color = xlNone
    End If
    Next
    End With
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
It would be better to use conditional formatting.
 
Upvote 0
i have applied this below vba to highlight cell color based on cell value (Pending) it's working when i am typing Pending in cell value of Column C Range & here i have created a drop down list with text Pending in Column C Range & when i selected Pending from drop down then the cell is not highlighting

Normally I would expect the code to look more like the below.

Having said that, it would not explain why it doesn't work when you select Pending from a dropdown.
The most likely issue is that the dropdown value is not exactly equal to Pending. Try typing =Len(Cell_Containing_Pending) and check that it returns 7 (and that there are no spaces in there)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.EnableEvents = False

    If Not Intersect(Target, Range("C1:C100")) Is Nothing Then

        If Target.Value = "Pending" Then
            Target.Interior.Color = vbRed
        Else
            Target.Interior.Color = xlNone
        End If

    End If
    
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Normally I would expect the code to look more like the below.

Having said that, it would not explain why it doesn't work when you select Pending from a dropdown.
The most likely issue is that the dropdown value is not exactly equal to Pending. Try typing =Len(Cell_Containing_Pending) and check that it returns 7 (and that there are no spaces in there)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
    Application.EnableEvents = False

    If Not Intersect(Target, Range("C1:C100")) Is Nothing Then

        If Target.Value = "Pending" Then
            Target.Interior.Color = vbRed
        Else
            Target.Interior.Color = xlNone
        End If

    End If
   
    Application.EnableEvents = True
End Sub
it's my mistake Alex

i have checked now in my data validation i have given space in (source Pending , Completed) & now my code is working i got that vba code in google search of stackoverflow
and i didn't applied your code in my worksheet module i will try it now
 
Upvote 0
Glad removing the space fixed the problem.
Let us know how you go with alternative code.

Your current code is going to run through a loop of all 100 cells in column C regardless of what was changed in the sheet eg any change anywhere on the sheet.
Typically you test to see if the cell you changed meets the criteria, in this case whether the Cell changed is within the range of C1:C100 and if it is you perform a certain action.
 
Upvote 0
Typically you test to see if the cell you changed meets the criteria, in this case whether the Cell changed is within the range of C1:C100 and if it is you perform a certain action
Yes I tested again that code if it will work or not

And Thankyou so much for written a code i have tested yours it's working perfectly
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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