Consulting

Results 1 to 9 of 9

Thread: Solved: highlighting matching cells

  1. #1
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location

    Solved: highlighting matching cells

    I have the below formula that highlights matching cells, but it also considers blank cells as mathing cells and highlights those as well and then labels those cash trades, which they arent.

    [VBA]Sub Cashtrades()
    Dim lr As Long, r As Long
    Dim cell As Range
    Dim str1 As String, str2 As String
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("A65000").End(xlUp).Row
    For Each cell In Range("A5:A" & lr)
    r = cell.Row

    If (StrComp(cell.Text, cell.Offset(0, 1).Text, vbTextCompare) = 0) Then

    Range("A" & r & ":P" & r).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0

    Set del = cell
    del.Offset(0, 16).FormulaR1C1 = "CASH TRADE"


    End With
    End If
    Next cell
    End Sub[/VBA]

    Is there a way to have it only highlight and label cash trades when there are values in the cells, not just blank cells?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just test for blank

    [vba]
    If cell.Text <> "" Then

    'other tests
    End If[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If they are constants, you could also check thus

    [vba]
    For Each cell In Range("A5:A" & lr).SpecialCells(xlCellTypeConstants)[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location

    see attached document

    See the attached document..this is a result of the below code and as you can see there are blank cells that are highlighted. My goal is to only have line 15 highlighted because the values in cells A15 and B15 match.

    [VBA]Sub Cashtrades()
    Dim lr As Long, r As Long
    Dim cell As Range
    Dim str1 As String, str2 As String
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("A65000").End(xlUp).Row
    For Each cell In Range("A5:A" & lr)
    r = cell.Row
    If (StrComp(cell.Text, cell.Offset(0, 1).Text, vbTextCompare) = 0) Then

    Range("A" & r & ":P" & r).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0

    Set del = cell
    del.Offset(0, 16).FormulaR1C1 = "CASH TRADE"

    End With
    End If
    Next cell
    End Sub[/VBA]
    Attached Files Attached Files

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Did you try either of my suggestions?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    I tried the below but i get the same results as before:

    [VBA]Sub Cashtrades()
    Dim lr As Long, r As Long
    Dim cell As Range
    Dim str1 As String, str2 As String
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("A65000").End(xlUp).Row
    For Each cell In Range("A5:A" & lr).SpecialCells(xlCellTypeConstants)
    r = cell.Row
    If (StrComp(cell.Text, cell.Offset(0, 1).Text, vbTextCompare) = 0) Then

    Range("A" & r & ":P" & r).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0

    Set del = cell
    del.Offset(0, 16).FormulaR1C1 = "CASH TRADE"

    End With
    End If
    Next cell
    End Sub[/VBA]

  7. #7
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    And then when i test for blank i keep getting the error end if without block if..

    I think i am putting these codes in the wrong places?

    [VBA]Sub Cashtrades()
    Dim lr As Long, r As Long
    Dim cell As Range
    Dim str1 As String, str2 As String
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("A65000").End(xlUp).Row
    For Each cell In Range("A5:A" & lr).SpecialCells(xlCellTypeConstants)
    r = cell.Row

    If cell.Text <> "" Then


    If (StrComp(cell.Text, cell.Offset(0, 1).Text, vbTextCompare) = 0) Then

    Range("A" & r & ":P" & r).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End If

    Set del = cell
    del.Offset(0, 16).FormulaR1C1 = "CASH TRADE"

    End With
    End If
    Next cell
    End Sub[/VBA]

  8. #8
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    I figured it out.. see the below solution.

    [VBA]Sub Cashtrades()
    Dim lr As Long, r As Long
    Dim cell As Range
    Dim str1 As String, str2 As String
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("A65000").End(xlUp).Row
    For Each cell In Range("A5:A" & lr)
    r = cell.Row

    If cell.Text <> "" And (StrComp(cell.Text, cell.Offset(0, 1).Text, vbTextCompare) = 0) Then

    Range("A" & r & ":P" & r).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0

    Set del = cell
    del.Offset(0, 16).FormulaR1C1 = "CASH TRADE"

    End With
    End If
    Next cell
    End Sub[/VBA]

    Thanks for all your help!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Klartigue
    And then when i test for blank i keep getting the error end if without block if..

    I think i am putting these codes in the wrong places?
    Just look at your construct pairings - should be self-evident.

    You have

    [vba]
    If ....

    With ...

    ...
    End If

    ...
    End With[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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