Consulting

Results 1 to 3 of 3

Thread: Simple VBA question

  1. #1
    VBAX Regular
    Joined
    Mar 2009
    Posts
    25
    Location

    Simple VBA question

    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").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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Cell As Range
    Dim Rng1 As Range

    On Error Resume Next
    Set Rng1 = Me.Columns("D").SpecialCells(xlCellTypeFormulas)
    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    I suspect this'll do what you want:[vba]Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    Dim Rng1 As Range
    Set Rng1 = Intersect(Range("D"), Target)
    If Not Rng1 Is Nothing Then
    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 If
    End Sub
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •