Consulting

Results 1 to 9 of 9

Thread: Skip blank cells or cells with text

  1. #1

    Skip blank cells or cells with text

    I've simplified my requirements (compared to a previous thread) and found some code that partially works. (I found it here on VBAX, but I can't remember who's original work it is, so who ever created it, I appreciate it.) Now I have a start, but need a few changes.

    I'd like this to only change the interior color of the cells with a number in them, and to ignore the cells that are blank or have any text in them. Also some cells have a different interior color already, if possible I'd like it to retain it's color. The following code changes the interior color to white for all cells that are not a duplicate.

    Here's what I have so far

    [VBA]Sub MakeDupsRed()

    Dim myRange As Range
    Dim strColToSort As String
    'Note: first cell in column is considered a heading
    'and is not included in the filter.
    strColToSort = "D"
    Application.ScreenUpdating = False
    Set myRange = Columns(strColToSort & ":" & strColToSort)
    myRange.Interior.ColorIndex = 3
    myRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    myRange.Interior.ColorIndex = xlNone
    ActiveSheet.ShowAllData
    Application.ScreenUpdating = True
    End Sub[/VBA]


    Thanks
    Rolly

  2. #2
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Well, it is not very good, but works...
    [VBA]Sub MakeDupsRed()

    Dim myRange As Range, difRange As Range
    Dim strColToSort As String

    'Note: first cell in column is considered a heading
    'and is not included in the filter.

    strColToSort = "D"
    Application.ScreenUpdating = False

    Set myRange = Columns(strColToSort & ":" & strColToSort)
    myRange.Interior.ColorIndex = 3
    myRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True

    Set difRange = Range(strColToSort & "1") ' I put that only to avoid an error in the first
    ' iteration on the loop below.

    For Each cell In myRange.Cells
    If IsNumeric(cell) Then
    Set difRange = Application.Union(difRange, Range(cell.Address))
    End If
    Next cell

    myRange.Interior.ColorIndex = xlNone
    ActiveSheet.ShowAllData
    difRange.Interior.ColorIndex = xlNone
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  3. #3
    Thanks,

    It works better than mine, the blank cells are now no longer red, but now it's not changing red when I intentionally add a duplicate number to test it.

    Rolly

  4. #4
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Let me see if I got it:

    You want only duplicated numbers to be colored on red, am I right?

  5. #5
    correct

  6. #6
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Would it be this?
    [VBA]
    Sub MakeDupsRed()

    Dim myRange As Range, difRange As Range
    Dim strColToSort As String

    strColToSort = "D"
    Application.ScreenUpdating = False

    Set myRange = Columns(strColToSort & ":" & strColToSort)
    myRange.Interior.ColorIndex = 3
    myRange.SpecialCells(xlBlanks).Interior.ColorIndex = xlNone
    myRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True

    For Each cell In myRange.Cells '*
    If Not IsNumeric(cell) Then '*
    cell.Interior.ColorIndex = xlNone '*
    End If '*
    Next cell '*

    myRange.Interior.ColorIndex = xlNone
    ActiveSheet.ShowAllData
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

    Comment: I believe it is possible writing commented lines in a better way.

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Benzadeus's code works but to get it to work when you input numbers you need to put it in the code for the module as a worksheet Change event.

    Benzadeus, I also changed the name of your variable cell to cel to cut down on confusion, not only by users but by the machinary also. cell is an object in excel so we shouldn't use it as a variable......other than that it works great.

    [VBA]
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myRange As Range, difRange As Range
    Dim strColToSort As String
    Dim cel As Range

    strColToSort = "D"
    Application.ScreenUpdating = False

    Set myRange = Columns(strColToSort & ":" & strColToSort)
    myRange.Interior.ColorIndex = 3
    myRange.SpecialCells(xlBlanks).Interior.ColorIndex = xlNone
    myRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True

    For Each cel In myRange.Cells '*
    If Not IsNumeric(cel) Then '*
    cel.Interior.ColorIndex = xlNone '*
    End If '*
    Next cel '*

    myRange.Interior.ColorIndex = xlNone
    ActiveSheet.ShowAllData
    Application.ScreenUpdating = True
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Benzadeus's code works but to get it to work when you input numbers you need to put it in the code for the module as a worksheet Change event.
    Hehehe I didn't understand that the code was suppose to do that.


    Hey Lucas!

    I believe "Cell" isn't an object, "Cells" is it (at least at Excel 2003).


    Comment: I believe it is possible writing commented lines in a better way, like selecting all interior.colorindex=xlnone cells at once.

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You're right Benzadeus.......cells.......still confusing especially to new people.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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