+ Reply to Thread
Results 1 to 16 of 16

colouring the cell based on the values of other column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    colouring the cell based on the values of other column

    Hi Excel gurus,


    i have two columns A and B

    In A (code number) and in B (extn)

    A is unique and have more extn assigned to it

    All the extn assigned to code number should be equal in Length.

    I need a macro to colour a code number if its corresponding all the extn are not equal in size.

    i have attached with the example.colouring the cell based on the values of other column.xlsx

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: colouring the cell based on the values of other column

    Shall we use Conditional Formatting?

    Please confirm


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    yes.. any how if done by macro means that would be great...

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: colouring the cell based on the values of other column

    Hi,

    This is my approach, using macro.

    Sub FilterIt()
      Dim rng As Range, cell As Range, mtx(), i As Long
      Set rng = Selection
      ReDim mtx(1 To 2, 1 To rng.Rows.Count)
      'Get uniques
      i = 0
      rng.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        For Each cell In rng.SpecialCells(xlCellTypeVisible)
            i = i + 1
            mtx(1, i) = cell.Value
            If i > 1 Then
               If mtx(1, i) = mtx(1, i - 1) Then
                  mtx(1, i) = ""
                  i = i - 1
               End If
               If mtx(1, i) = "" Then i = i - 1
            End If
        Next cell
        ReDim Preserve mtx(1 To 2, 1 To i)
      rng.AdvancedFilter Action:=xlFilterInPlace, Unique:=False
      'Check length
      For Each cell In rng
          For i = 1 To UBound(mtx, 2)
              If cell.Value = mtx(1, i) Then
                 If IsEmpty(mtx(2, i)) Then
                    mtx(2, i) = Len(cell.Offset(, 1))
                 Else
                    If Len(cell.Offset(, 1).Value) <> mtx(2, i) Then
                       mtx(2, i) = -1
                       Exit For
                    End If
                 End If
              End If
          Next i
      Next cell
      'Set colour
      For Each cell In rng
          For i = 1 To UBound(mtx, 2)
              If cell.Value = mtx(1, i) Then
                 If mtx(2, i) = -1 Then cell.Interior.Color = 65535
                 Exit For
              End If
          Next i
      Next cell
    End Sub
    Select the range (A4:A18) then run the macro.
    You don't even need to sort the data first, look at my attached file, and try to run at range A23:A39.

    Regards
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  5. #5
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    John H Davis ... thanks for the helps. ur code works good.


    Karedog .... thanks for ur help and quick reply.....

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: colouring the cell based on the values of other column

    Maybe:

    Sub excelhelpseeker()
    Dim i As Long
    Dim y As Long
    Dim x As String
    For i = Range("A" & Rows.Count).End(3).Row To 3 Step -1
        If Range("A" & i) <> "" Then
            x = Range("A" & i).Value
                For y = i - 1 To 3 Step -1
                    If Range("A" & y) = x And Len(Range("B" & y)) <> Len(Range("B" & i)) Then
                        Range("A" & i).Interior.ColorIndex = 6
                            Range("A" & y).Select
                                Do Until ActiveCell.Row = 3
                                    If ActiveCell.Value = x Then
                                        ActiveCell.Interior.ColorIndex = 6
                                    End If
                                    ActiveCell.Offset(-1).Select
                                Loop
                    End If
                Next y
        End If
    Next i
    End Sub

  7. #7
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    shows error at ....................mtx(1, i) = cell.Value

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: colouring the cell based on the values of other column

    Quote Originally Posted by excelhelpseeker View Post
    shows error at ....................mtx(1, i) = cell.Value
    You ***MUST NOT*** select A2, just select A4:A18 (or A23:A39) as I told in my previous post.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: colouring the cell based on the values of other column

    You're welcome. Glad to help out and thanks for the feedback.

  10. #10
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: colouring the cell based on the values of other column

    Or

    Sub ColorRangeOfCells()
    Dim rSource As Range, r As Range, lTot As Long, lReq As Long, sR1 As String, sR2 As String
    
    Set rSource = Application.InputBox("Select Data", "Data Seleciton Required", "=Sheet1!A4:B18", , , , , 8)
    
    With rSource
        sR1 = "'" & .Parent.Name & "'!" & .Columns(1).Address
        sR2 = "'" & .Parent.Name & "'!" & .Columns(2).Address
    End With
    
    
    For Each r In rSource.Columns(1).Cells
        If r.Value <> "" Then
            e = "SUMPRODUCT((" & sR1 & "=" & r.Value & ")*LEN(" & sR2 & "))"
            lTot = Evaluate("SUMPRODUCT((" & sR1 & "=" & r.Value & ")*LEN(" & sR2 & "))")
            lReq = Evaluate("COUNTIF(" & sR1 & "," & r.Value & _
                    ")*MAX(INDEX((" & sR1 & "=" & r.Value & ")*LEN(" & sR2 & "),))")
            If lTot <> lReq Then r.Resize(, rSource.Columns.Count).Interior.Color = 65535
        End If
    Next r
    
    End Sub

  11. #11
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    hi john,, the macro is fine with the example i provided but doesnot work as i have many codes with atleast 6 or 7 extn for a single code

  12. #12
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: colouring the cell based on the values of other column

    Not sure I understand. Can you post a sample where it doesn't work?

  13. #13
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    sample.xlsxyes john i have attached the sample file. pls note that i have empty cells in code or extn....

  14. #14
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: colouring the cell based on the values of other column

    In your sample from Post#13 with the exception for "QUERY" in Column A all the items are unique. Why would you expect it too work? It's an entire different lay out than what you had from Post #1? If you interchange the columns, it does highlight. Not sure that's what you are looking for though?
    Last edited by JOHN H. DAVIS; 11-17-2014 at 10:58 AM.

  15. #15
    Forum Contributor
    Join Date
    11-16-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    204

    Re: colouring the cell based on the values of other column

    but the code is at column B...

  16. #16
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: colouring the cell based on the values of other column

    This switches too Column B?

    Sub excelhelpseekerzz()
    Dim i As Long
    Dim y As Long
    Dim x As String
    For i = Range("B" & Rows.Count).End(3).Row To 4 Step -1
        If Range("B" & i) <> "" Then
            x = Range("B" & i).Value
                For y = i - 1 To 3 Step -1
                    If Range("B" & y) = x And Len(Range("A" & y)) <> Len(Range("A" & i)) Then
                        Range("B" & i).Interior.ColorIndex = 6
                            Range("B" & y).Select
                                Do Until ActiveCell.Row = 3
                                    If ActiveCell.Value = x Then
                                        ActiveCell.Interior.ColorIndex = 6
                                    End If
                                    ActiveCell.Offset(-1).Select
                                Loop
                    End If
                Next y
        End If
    Next i
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Colouring areas depending on column values
    By tuppari in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-10-2012, 06:58 PM
  2. [SOLVED] Colouring cells based on a cell value
    By London257 in forum Excel General
    Replies: 2
    Last Post: 10-25-2012, 04:56 AM
  3. Colouring rows based on their values
    By alocke in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-16-2012, 06:36 PM
  4. Replies: 5
    Last Post: 02-07-2012, 08:06 AM
  5. Colouring Tabs Based on a Cell Value
    By TBrooker in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-28-2009, 08:09 AM

Tags for this Thread

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