Simple VBA code guidance wanted

percy83

Active Member
Joined
Mar 11, 2009
Messages
278
Hi all VBA experts,

I am trying to use VBA to apply conditional formatting to column “D” but the problem is that all cells are affected by my VBA code and just not only column “D”. I am a total noob at VBA and its probably ridiculously easy to fix it but I still need your help.
Please see code below.
Best regards
Per nilsson

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Cell As Range
Dim Rng1 As Range

On Error Resume Next
Set Rng1 = ActiveSheet.Range("D:D").Special(xlCellTypeFormulas, 1)
On Error GoTo 0
If Rng1 Is Nothing Then
Set Rng1 = Range(Target.Address)
Else
Set Rng1 = Union(Range(Target.Address), Rng1)
End If
For Each Cell In Rng1
Select Case Cell.Value
Case vbNullString
Cell.Interior.ColorIndex = xlNone
Cell.Font.Bold = False
Case "Black"
Cell.Interior.ColorIndex = 1
Cell.Font.Name = "arial narrow"
Cell.Font.Bold = True
Cell.Font.ColorIndex = 2

Case "Blue"
Cell.Interior.ColorIndex = 5
Cell.Font.Name = "arial narrow"
Cell.Font.Bold = True
Cell.Font.ColorIndex = 2

Case "Red"
Cell.Interior.ColorIndex = 3
Cell.Font.Name = "arial narrow"
Cell.Font.Bold = True
Cell.Font.ColorIndex = 2

Case Else
Cell.Interior.ColorIndex = xlNone
Cell.Font.Bold = False
End Select
Next

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
if you want a humble opinion, conditional formatting is best done through the interface.
 
Upvote 0
yeah Mr. Trumpet is right. However, I guess if you have more than three conditions you can prolly put your code in an if statement that checks to see if the Target column is the 4th Column (i.e. Column D) and if it is your conditional statements are ran Else nothing happens. Eg:

Code:
If Target.Column = 4 Then
        'Do Something
    Else
        'Do Nothing
End If
 
Upvote 0
:) We don't want to go down that path (again). The world would be a far better place without such fairytales. It is ironic how something which would/should be a good thing (or at least one would think) has been twisted by mankind to such a point that it pits different ideologies against each other to the point where our brothers and sisters are willing to take each others lives and to the point where all rational thought is lost.

I think I'm also right by COMPLETELY reversing your signature. E.g: RIGHT TO LEFT. LOL
 
Upvote 0
Good point about killing each other. Not a cool thing. CONVERSATION ENDED (or in today's language, PROGRAM TERMINATED). :)
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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