Consulting

Results 1 to 12 of 12

Thread: color cell

  1. #1
    VBAX Regular achin's Avatar
    Joined
    Apr 2008
    Posts
    13
    Location

    color cell

    Hi,
    the following code is not working.
    all i want is...
    somebody help plz
    when i key in "1" in A1 of sheet 1, it'll colored B1:C3 and E2,E3 in blue+bold+white font
    when i key in "2" in A1 of sheet 1, it'll colored B1:C3 and E2,E3 in green+bold+white font
    when i key in "3" in A1 of sheet 1, it'll colored B1:C3 and E2,E3 in light yellow+bold+black font

    [VBA]
    Private Sub worksheet_change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim CellVal As String
    If Target.Cells.Count > 1 Then Exit Sub
    CellVal = Target
    Set WatchRange = Workbooks("Book1").Worksheets("Sheet1").Range("A1")
    'change to suit
    If Not Intersect(Target, WatchRange) Is Nothing Then
    Select Case CellVal
    Case "1"
    Target.Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    Case "2"
    Target.Range("B1:C3, E2,E3").Interior.ColorIndex = 10
    Case "3"
    Target.Range("B1:C3, E2,E3").Interior.ColorIndex = 6
    Case "4"
    Target.Range("B1:C3, E2,E3").Interior.ColorIndex = 46
    Case "5"
    Target.Range("B1:C3, E2,E3").Interior.ColorIndex = 45
    Case Empty, ""
    Target.EntireRow.Interior.ColorIndex = xlColorIndexNone
    End Select
    End If
    End Sub
    [/VBA]

  2. #2
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    Should work now:

    [VBA]Sub worksheet_change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim CellVal As String
    If Target.Cells.Count > 1 Then Exit Sub
    CellVal = Target
    Set WatchRange = Workbooks("Book4").Worksheets("Sheet1").Range("A1")
    'change to suit
    If Not Intersect(Target, WatchRange) Is Nothing Then
    Select Case CellVal
    Case "1"
    With Target.Range("B1:C3, E2,E3")
    .Interior.ColorIndex = 5
    .Font.Bold = True
    .Font.ColorIndex = 2
    End With
    Case "2"
    With Target.Range("B1:C3, E2,E3")
    .Interior.ColorIndex = 10
    .Font.Bold = True
    .Font.ColorIndex = 2
    End With
    Case "3"
    With Target.Range("B1:C3, E2,E3")
    .Interior.ColorIndex = 6
    .Font.Bold = True
    .Font.ColorIndex = 0
    End With
    Case "4"
    With Target.Range("B1:C3, E2,E3")
    .Interior.ColorIndex = 46
    .Font.Bold = True
    .Font.ColorIndex = 0
    End With
    Case "5"
    With Target.Range("B1:C3, E2,E3")
    .Interior.ColorIndex = 45
    .Font.Bold = True
    .Font.ColorIndex = 0
    End With
    Case Empty, ""
    Target.EntireRow.Interior.ColorIndex = xlColorIndexNone
    End Select
    End If
    End Sub
    [/VBA]

  3. #3
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    5
    Location

    It's not working

    Hi, Maxims..
    I've tried to compile with your codes.
    But it's promt our an error message:Run Time Error '1004'
    It's about the error caused by the Intersect Method.

    Any solution on this?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    With only three conditions, use Conditional Formatting
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    5
    Location

    more than 3 conditional formatting

    I have 15 to 20 conditionals here.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]Private Sub worksheet_change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address(0, 0) <> "A1" Then Exit Sub
    Select Case Target
    Case "1"
    Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    Case "2"
    Range("B1:C3, E2,E3").Interior.ColorIndex = 10
    Case "3"
    Range("B1:C3, E2,E3").Interior.ColorIndex = 6
    Case "4"
    Range("B1:C3, E2,E3").Interior.ColorIndex = 46
    Case "5"
    Range("B1:C3, E2,E3").Interior.ColorIndex = 45
    Case Empty, ""
    EntireRow.Interior.ColorIndex = xlColorIndexNone
    End Select
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Using this code
    [VBA]With Target.Range("B1:C3, E2,E3")[/VBA]
    will affect the cells RELATIVE to Target. If Target is not A1 then you may get unexpected results.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    5
    Location
    mdmackillop, the input is on worksheet 1 but output formatting on worksheet 2.

  9. #9
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    Assuming sheet1 is the input sheet something like this should work![VBA]Case "1"
    Sheets("Sheet2").Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  10. #10
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    5
    Location
    thank you all very much!

  11. #11
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    5
    Location
    how about if i need it to update sheet 3 at the same time when sheet1 is the input sheet ?

  12. #12
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    .[VBA]
    Case "1"
    Sheets("Sheet2").Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    Sheets("Sheet3").Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    Sheets("Sheet4").Range("B1:C3, E2,E3").Interior.ColorIndex = 5
    .
    .
    [/VBA]

Posting Permissions

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