Home » excel » How can I find the last colored & empty cell in a row using VBA in Excel 2010?

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

  1. When trying to identify the last cell/row/column, it is better to start from the bottom or the right.
  2. The Find method is faster than using a loop.
  3. Questions about (and thus usage of) Find focus on identifying the last row or column with (sometimes without) data, or the last cell used (typically in a column, and containing data).
  4. It is best to specify the interior color before using the Find method because it may have the value of some other interior color from previous time it was used (by code or by user).

Here is the code I have thus 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 my novice eye in apparent conflict with the search direction I want (After indicates to the right (?), xlPrevious says go to the left). If the search must indeed start 1 cell to the right before proceeding to the left, I specified the next-to-the-last cell in the row.

It also is likely I can restrict the search of the row to the first 8785 columns.

I’m trying at the end (this time using Select) to determine if the code is identifying the correct cell. It is at this point when stepping through the code I get the error:

Run-time error ’91’:Object variable or With block variable not set

  1. How can I determine if the code is identifying the correct cell? (I’ve tried MsgBox and setting the interior color of the found cell to a different color, but get errors as well.)
  2. Is there an error in the code preventing it from identifying the correct cell?
  3. Should I instead use an approach other than the Find method?

I look forward to (and would be most thankful for) any ‘gurus’ cracking this little nut!

How to&Answers:

Well asked and laid out question.

  1. Test that you have found a match with If Not rngFindColorCell Is Nothing (see my new last line below)
  2. Your code as it stands starts looking first at XFB18 as you have used xlPrevious, then at XFA18 etc (eventually reaching back to XFD18 and finally XFC18
  3. Your Select line is unnecessary.
  4. Best to use xlWhole when macthing an entire string rather than xlPart (won’t matter in this case)
  5. Using xlFormulas rather than xlValues will 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