Consulting

Results 1 to 5 of 5

Thread: VBA Celloffset function in For Each Cell function not work

  1. #1

    Question VBA Celloffset function in For Each Cell function not work

    Hi,
    Can someone help me check the below code? The first sub works, it changes the interior color of duplicate cells in selection.
    However, the second one not work because of the if function.
    The offset and interior condition both not work.

    Thank you!
    ---------------------------------------------------------------------------------------------------
    Sub dup()
    ' dup Macro
        Selection.FormatConditions.AddUniqueValues
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        Selection.FormatConditions(1).DupeUnique = xlDuplicate
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
    Call Bold
    End Sub
    'wrong one but right intention
    Sub Bold()
    For Each cell In Selection
    If cell.Interior.ColorIndex = 255 Then
      cell.Offset(-4, 0).Font.Bold = True
    End If
    Next cell
    End Sub
    Last edited by SamT; 01-20-2017 at 04:49 PM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    FormatCondition does not effect the Interior of Ranges. IOW, the Cells Interior Colors and ColorIndexes have not been set.

    ColorIndexes can only be 1 to 56 and a couple of constants that mean no color.
    Colors can be any of a few vbConstants or any RGB color code
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Sub dup()
         ' dup Macro
        Selection.FormatConditions.AddUniqueValues
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        Selection.FormatConditions(1).DupeUnique = xlDuplicate
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        Call Bold
    End Sub
    Sub Bold()
    For Each cell In Selection
      If cell.DisplayFormat.Interior.Color = 255 Then
        cell.Offset(-4, 0).Font.Bold = True
      End If
    Next cell
    End Sub
    Realise that
    cell.Offset(-4, 0).Font.Bold = True
    boldens the cell 4 cells ABOVE the selection.
    The only changes are:
    If cell.DisplayFormat.Interior.Color = 255 Then
    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.

  4. #4
    Thank you SamT for the suggestions. I was confused by the colorindex before but now I understand.
    Thanks!

  5. #5
    Thank you so much P45cal!!! Your code works perfectly. I can appreciate anymore

Posting Permissions

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