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:
- When trying to identify the last cell/row/column, it is better to start from the bottom or the right.
- The Find method is faster than using a loop.
- Questions about (and thus usage of)
Findfocus on identifying the last row or column with (sometimes without) data, or the last cell used (typically in a column, and containing data).
- It is best to specify the interior color before using the
Findmethod 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
- How can I determine if the code is identifying the correct cell? (I’ve tried
MsgBoxand setting the interior color of the found cell to a different color, but get errors as well.)
- Is there an error in the code preventing it from identifying the correct cell?
- 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!
Well asked and laid out question.
- Test that you have found a match with
If Not rngFindColorCell Is Nothing(see my new last line below)
- Your code as it stands starts looking first at
XFB18as you have used
xlPrevious, then at
XFA18etc (eventually reaching back to
Selectline is unnecessary.
- Best to use
xlWholewhen macthing an entire string rather than
xlPart(won’t matter in this case)
xlValueswill also match hidden columns (
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