VBA: Run-time error 1004. No cells were found.

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I just started getting this error today.

I'm building several macros that deal with Conditional Formatting (so far 20). They all use the following code to get the first empty cell in column A, which I'm using as my LastRow variable.

It wasn't until I got to Sheet 7 is when this issue occured. It's been persistent with any new Sheets I create. I even created a whole new workbook and the same issue occurred. However, this code runs flawlessly in Sheets 1 thru 6 in my VBA Testing workbook.

VBA Code:
Sub GetLastRow()
    Dim LastRow As Long, DataRange As Range
    With ActiveSheet
        LastRow = .Range("A2:A" & Rows.Count).SpecialCells(xlCellTypeBlanks).Offset(-1).Row 'Get first empty cell in column A -1
        Set DataRange = .Range("A3:A" & LastRow)
        MsgBox DataRange.Address
        MsgBox "Last Row is " & LastRow & " on Sheet: " & ActiveSheet.Name
    End With
End Sub

Here's a phenomenon to say the least:

Cell A21 is breaking the code in Sheet 7+. Here's why:

I can populate cells up to A20 and the code runs successfully.
Excel HAPPY

1678778427122.png


If I populate A21, the code breaks and throws that run-time error about how there are no cells.
Excel NOT Happy

1678778539590.png


If I populate A22 down (after clearing A21), the code runs successfully.
Excel Happy Again

1678778552032.png


I suspected some sort of corruption, so I ruled out memory, hard drive, and OS corruption.
I even did a fresh install of O365.

I'm stumped. This issue is nothing shy of an irritating inconsistency.

On the topic of 'cell", I noticed too, that I am unable to run any code that uses 'cell'. Mainly loops I believe; like this one:
This is just to serve as a potential symptom to my main issue.

VBA Code:
Sub Macro1()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cell In ws.Columns(1).Cells
        If IsEmpty(cell) = True Then cell.Select: Exit For
    Next cell
End Sub

1678778575314.png


Has anyone seen anything like this before?
Thanks for any insight.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I avoid using special cells to get the last row, i tend to use:
VBA Code:
LastRow = .Range("A2:A" & Rows.Count).End(xlUp).Row

As for the code below, i would not run code on an entire column but to help you understand what is wrong 'cell' should be defined as a range, i would avoid using 'cell' as it can be used by excel itself, i would use 'rCell' instead where the r stands for reference:
VBA Code:
Sub Macro1()
    Dim ws As Worksheet
    Dim rCell As Range
    
    Set ws = ActiveSheet
    For Each rCell In ws.Columns(1).Cells
        If IsEmpty(rCell) = True Then rCell.Select: Exit For
    Next rCell
End Sub
 
Upvote 0
VBA Code:
Sub GetLastRow()
    Dim LastRow As Long, DataRange As Range
    With ActiveSheet
        LastRow = .Range("A2:A" & Rows.Count).SpecialCells(xlCellTypeBlanks).Offset(-1).Row 'Get first empty cell in column A -1
        Set DataRange = .Range("A3:A" & LastRow)
        MsgBox DataRange.Address
        MsgBox "Last Row is " & LastRow & " on Sheet: " & ActiveSheet.Name
    End With
End Sub

If I populate A21, the code breaks and throws that run-time error about how there are no cells.
Excel NOT Happy

View attachment 87481

For this part of your question, its the normal behavior. Selecting blank cells only works in used range. It finds no blank cells in the used range. This will be safer if your column has inconsistent data:
VBA Code:
Sub GetLastRow()
    Dim LastRow As Long, DataRange As Range
    With ActiveSheet
        LastRow = .Range("A2").End(xlDown).Row 'Get first empty cell in column A -1
        Set DataRange = .Range("A3:A" & LastRow)
        MsgBox DataRange.Address
        MsgBox "Last Row is " & LastRow & " on Sheet: " & ActiveSheet.Name
    End With
End Sub

VBA Code:
Sub Macro1()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cell In ws.Columns(1).Cells
        If IsEmpty(cell) = True Then cell.Select: Exit For
    Next cell
End Sub

View attachment 87483
For this part of your question, I assume you are using Option Explicit. If yes, you have to declare cell As Range explicitly.
 
Upvote 0
Where can 'cell' be used by excel itself?

Like the below, even though it is a worksheet formula i still avoid using any variable names that are the same/ similar to existing excel functions:
Book1
AB
18TRUE
Sheet1
Cell Formulas
RangeFormula
A1:B1A1=CELL("width")
Dynamic array formulas.
 
Upvote 0
Like the below
OK, fair enough - but there is no overlap with vba so it is quite fine to use cell as a variable in vba. If you want to avoid it that's fine as a personal decision but no adverse consequences if others decide to use it as a variable name in vba. :)
 
Upvote 0
OK, fair enough - but there is no overlap with vba so it is quite fine to use cell as a variable in vba. If you want to avoid it that's fine as a personal decision but no adverse consequences if others decide to use it as a variable name in vba. :)

I make you right, it is a preference. Once you have encountered a few errors due to naming variables - it makes you steer well clear in the future.
 
Upvote 0
I avoid using special cells to get the last row, i tend to use:
VBA Code:
LastRow = .Range("A2:A" & Rows.Count).End(xlUp).Row

As for the code below, i would not run code on an entire column but to help you understand what is wrong 'cell' should be defined as a range, i would avoid using 'cell' as it can be used by excel itself, i would use 'rCell' instead where the r stands for reference:
VBA Code:
Sub Macro1()
    Dim ws As Worksheet
    Dim rCell As Range
 
    Set ws = ActiveSheet
    For Each rCell In ws.Columns(1).Cells
        If IsEmpty(rCell) = True Then rCell.Select: Exit For
    Next rCell
End Sub
Hi Georgi,

Normally I would agree with you, but that method skips over blank cells to the very bottom. For my dataset I can't go all the way down. It would try to include the Table Range that I'm using a few rows below my Standard Range.

My screenshots don't reflect my original data set. I had to scrub it and try some random values using a New Sheet to see if the code would error again; and it did.

Personally, I don't think there is anything wrong with the code. I suspect some sort of corruption. I just can't pinpoint it.

My reasoning is that the code runs flawlessly in 10 different Sub routines across the first 6 Sheets. It wasn't until Sheet 7 when I started having this issue.

The other reasons are the code only throws errors in Column A. For example:
On Sheet 7, cell A21 is the culprit.
On Sheet 8, it's cell A14.
...

*****************************************************************************************

OK, something just changed after I enabled the XL2BB Add-In to provide my sample data.
I need to check something out before I realize later it's just a fluke.

I'll be back...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top