+ Reply to Thread
Results 1 to 5 of 5

Adding conditional formatting

Hybrid View

stewart1 Adding conditional formatting 03-23-2011, 07:00 AM
snb Re: Adding conditional... 03-23-2011, 07:17 AM
stewart1 Re: Adding conditional... 03-23-2011, 07:24 AM
snb Re: Adding conditional... 03-23-2011, 07:26 AM
stewart1 Re: Adding conditional... 03-23-2011, 07:36 AM
  1. #1
    Registered User
    Join Date
    06-22-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    46

    Adding conditional formatting

    Good morning to all,

    I have conditional formatting code that works fine on its own but I have this existing code, courtesy of TMSchucks which provides a union of two existing changes. (below)
    Select Case Target.Column
    Case 3: Worksheet_ChangeC Target
    Case 17: Worksheet_ChangeQ Target
    Case Else
        MsgBox "should not get here"
        Exit Sub
    End Select
    
    End Sub
    
    Private Sub Worksheet_ChangeC(ByVal Target As Range)
    Dim c As Range, i As Long
    On Error Resume Next
    Set c = Intersect(Target, Columns(3))
    If c Is Nothing Then Exit Sub
    If IsEmpty(c.Offset(-1, 0)) Or Not IsEmpty(c.Offset(1, 0)) Then Exit Sub
    i = c.Row
    Application.EnableEvents = False
    Range("A" & i - 1 & ":B" & i - 1).Copy Range("A" & i & ":B" & i)
    Application.EnableEvents = True
    On Error GoTo 0
    End Sub
    
    Private Sub Worksheet_ChangeQ(ByVal Target As Range)
    Dim c As Range, i As Long
    On Error Resume Next
    Set c = Intersect(Target, Columns(17))
    If c Is Nothing Then Exit Sub
    If IsEmpty(c.Offset(1, 0)) Or Not IsEmpty(c.Offset(-1, 0)) Then Exit Sub
    i = c.Row
    Application.EnableEvents = False
    Range("Q" & i - 1).Copy Range("Q" & i)
    Application.EnableEvents = True
    On Error GoTo 0
    End Sub
    The code I have for the conditional formatting is below:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim icolor As Integer
    
    
    
    	If Not Intersect(Target, Range("Q3:Q60000")) is Nothing Then
    
    		Select Case Target
    
    			Case 1 To 6
    
    				icolor = 4
    
    			Case 7 To 10
    
    				icolor = 45
    
    			Case 11 To 1000000000000
    
    				icolor = 3
    
    			Case Else
    
    				'Whatever
    
    		End Select
    
    		
    
    		Target.Interior.ColorIndex = icolor
    
    	End If
    
    
    
    End Sub
    I can see that I could modify the existing code to suit if the conditional formatting code referred to a specific column, (I would choose case 17) but it is a range, so I am a bit stuck of how to combine it.

    Any guidance much appreciated.

    Thank you.
    Last edited by stewart1; 03-23-2011 at 07:40 AM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Adding conditional formatting

    Simpler ?
    Private Sub Worksheet_Change(ByVal Target As Range)
      if target.column=3 or target.column=17 then range("A1:B1").offset(target.row-1)=range("A1:B1").offset(target.row-2).value
    End sub
    It should perform the same task as the code TMShucks offered.



  3. #3
    Registered User
    Join Date
    06-22-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Adding conditional formatting

    Hi snb,

    Thanks for your reply. Without sounding like a complete muppett, where would i put that code into existing changes without conflict.

    Apologies in advance if I have missed the obvious!

    Stewart

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Adding conditional formatting

    It's meant to replace all THShucks's code.

  5. #5
    Registered User
    Join Date
    06-22-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Adding conditional formatting

    Hi snb,

    I now see. Thank you for a swift result!

    Stewart

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1