How can I find the last colored and blank cell in a row using VBA in Excel 2010?

I am new to VBA and am writing a macro to find the last blank cell filled with a specific color on a specific row. Based on what I've read and tried so far:

  • When trying to identify the last cell / row / column, it's best to start at the bottom or to the right.
  • The Find method is faster than using a loop.
  • Questions about (and therefore usage) Find

    focus on identifying the last row or column with (sometimes missing) data, or the last cell used (usually in a column and containing data).
  • It is best to specify the interior color before using the method Find

    , because it can mean some other interior color from previous use (by code or user).

Here is the code I have so far:

Sub FindLastEmptyTurquoiseCellOnRow18()
Dim rngFindColorCell As Range
    Range("XFD18").Select
    With Application.FindFormat.Interior
        .Color = 16763955
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With
    Set rngFindColorCell = Range("A18:XFD18").Find(What:="", After:=Range("XFC18"), _
     LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
     MatchCase:=False, SearchFormat:=True)
    rngFindColorCell.Select
End Sub

      

The After parameter seems to be my newbie in explicit conflict with the search direction I want (after points to the right (?), xlPrevious

Says to go left). If the search really needs to start 1 cell from the right before going to the left, I've specified the next last cell in the row.

Also, I can probably limit the search for a row to the first 8785 columns.

I am trying at the end (use this time Select

) to determine if the code identifies the correct cell. It is at this point when I step through the code that I get the error:

Runtime error '91': object variable or with locked block variable

  • How to determine if a code identifies the correct cell? (I tried MsgBox

    and set the interior color of the found cell to a different color, but also got errors.)
  • Is there a bug in the code that prevents the correct cell from being identified?
  • Should I use a method other than the Find method instead?

I look forward to (and would be very grateful) for any "gurus" cracking this little nut!

+3


source to share


1 answer


Well asked and laid out question.

  • Check you found a match with If Not rngFindColorCell Is Nothing

    (see my new last line below)
  • Your code at the beginning starts looking first at XFB18

    how you used xlPrevious

    , then at XFA18

    , etc. (eventually back to XFD18

    and finallyXFC18

  • Your string Select

    is unnecessary.
  • It is best to use a xlWhole

    whole string when macthing rather than xlPart

    (in this case it doesn't matter)
  • Using xlFormulas

    and will not xlValues

    also match hidden columns ( xlValues

    skips them)


code

Sub FindLastEmptyTurquoiseCellOnRow18()
Dim rngFindColorCell As Range

With Application.FindFormat.Interior
        .Color = 16763955
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
End With

Set rngFindColorCell = Range("A18:XFD18").Find(vbNullString, Range("XFC18"), xlFormulas, xlWhole, xlByRows, xlPrevious, , True)

If Not rngFindColorCell Is Nothing Then
  MsgBox "found in " & rngFindColorCell.Address(0, 0)
Else
  MsgBox "No matching cells found"
End If

End Sub

      

+2


source







All Articles