VBA If criteria

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to provide a specific criteria, if I delete/remove a value in Column F and Column B has a color index of 3 with the text "1299" that it will return Column B to a color index of xlNone and clear the contents of the "1299", when the Column F value has been removed with its specific criteria.

Capture.PNG



I've managed to provide so far where if I delete the value in Column F that it clears the contents of the "1299" of Column B but I cannot remove the interior color of 3 to xlNone.

Here is a sample of my code that I'm working on:

VBA Code:
Private Sub MessageColumnB(ByVal Target As Range)

    Dim n As Long

  n = Target.row

    
If Range("B" & n).Interior.ColorIndex = 3 _
    And Range("B" & n) = "1299" _
    And Range("F" & n) = "" Then
          
          Cells(n, "B").ClearContents
          
   'Cells(n, "B").Interior.ColorIndex = xlNone       =>? - How do I add this part to it?
    
    End If
    
    
End Sub


Many thanks in advance!

Pinaceous
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I realize that

VBA Code:
Cells(n, "B").Clear

can be an option, but then how would I keep the formatting of the original cell?
 
Upvote 0
try this simple modification:
VBA Code:
Private Sub MessageColumnB(ByVal Target As Range)

    Dim n As Long

  n = Target.Row

    
If Range("B" & n).Interior.ColorIndex = 3 _
    And Range("B" & n) = "1299" _
    And Range("F" & n) = "" Then
          
          Cells(n, "B").ClearContents
          Range("B" & n).Interior.ColorIndex = 0
   'Cells(n, "B").Interior.ColorIndex = xlNone       =>? - How do I add this part to it?
    
    End If
    
    
End Sub
 
Upvote 0
Hey offthelip,

Thank you for that suggestion! It works great! Would you know how I can also set up an additional criteria with Column B, where if a number value other than 1299 is entered into Column B that it will remove its interior color red back to xlNone and keep that value entered?

For example, if a user wants to override the Column B interior color red with its value of "1299" with a value between (1 to 2359 except 1299) with ie."1234" that it will remove the interior cell condition and keep that new value?

For example, if I have this condition below and I want to type over the "1299" value with 12:34 that it will provide this progression.

Capture.PNG


Please let me know, thanks!
 
Upvote 0
You could just use conditional fromatting on cell B2 to change the color of the cell.
However if you want to do it in vba try this:
VBA Code:
Private Sub MessageColumnB(ByVal Target As Range)
 Dim n As Long
 n = Target.Row
   
If Range("B" & n).Interior.ColorIndex = 3 _
    And Range("B" & n) = "1299" _
    And Range("F" & n) = "" Then
          Cells(n, "B").ClearContents
          Range("B" & n).Interior.ColorIndex = 0
 End If
' add this bit
 If Range("B" & n) <> "1299" Then
          Range("B" & n).Interior.ColorIndex = 0
 End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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