Can anyone advise on how to handle a "no cells were found" error with the following code. This is a part of a larger sub that may often return no values, however handling the error as follows (which works for many of my other scenarios) still returns "Run-time error '1004': No cells were found". What am I doing wrong?
On Error GoTo Error_Exit_3
Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
Error_Exit_3:
What I Would do:
Dim RowsWithFormulas As Long
On Error Resume Next
RowsWithFormulas = Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).Rows.Count
On Error GoTo 0
If RowsWithFormulas > 0 Then
Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
End If
You can also set this as a Range:
Sub t()
Dim cellsWithErroringFormulas As Range
On Error Resume Next
Set cellsWithErroringFormulas = Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
If cellsWithErroringFormulas Is Nothing Then
' Do whatever
MsgBox ("No formulas result in an error!")
Exit Sub
ElseIf cellsWithErroringFormulas.Rows.Count > 0 Then
cellsWithErroringFormulas.SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
' Now, if you call `cellsWithErroringFormulas` again, it will error since you removed all those references.
' So to be explicit, let's clear that variable.
Set cellsWithErroringFormulas = Nothing
End If
End Sub
I tweaked the variable name, just because you're not technically looking for rows with formulas, but rather cells with formulas that result in an error. It's a little clunky here, so rename as desired. Just wanted to point that out.
Also, since I don't know what you plan on doing next, I added the Set cellsWithErroringFormulas = Nothing, since we can't use that reference after you delete the erroring rows. You may not need that, but I just wanted to include to point that out also.
Please check the link below. its already answered in this forum.
Also, Please visit the rules for this forum.. :)
1004 Error: No cells were found, easy solution?
Related
When using Range.SpecialCells with a range that doesn't contain cells that match the criteria an error is thrown saying that no cells were found.
The most common solution to this issue is letting it happen and using an error handler to deal with it.
Is that the best known way to solve it or is there other solutions that might be as good or better which avoid using an error handler?
The only thing I can think of would be saving the first cell's value, then changing its value to one that matches the criteria so it avoids the error making it always match at least that one cell, then change the value back to its original value and check the matched range's address to see if it matched that one cell only or more.
A bad/slow solution would be to not make use of it at all and just use loops with checks.
Here's some simple sample code to demostrate a little how it works with the error handler:
Private Sub Procedure()
Dim OriginalRange As Excel.Range
Dim NewRange As Excel.Range
Set OriginalRange = ThisWorkbook.Worksheets(1).Range("A1:C4")
On Error GoTo ErrorHandler
Set NewRange = OriginalRange.SpecialCells(Type:=Excel.XlCellType.xlCellTypeConstants, Value:=Excel.XlSpecialCellsValue.xlNumbers)
Exit Sub
ErrorHandler:
If (VBA.Err.Number <> 1004) Then VBA.Err.Raise VBA.Err.Number
End Sub
Yes it is perfectly normal (I prefer this way) to use the error handler. What I do is, I sandwhich it between On Error Resume Next and On Error GoTo 0 and then check If NewRange is Nothing
See this example
On Error Resume Next
Set NewRange = OriginalRange.SpecialCells(Type:=Excel.XlCellType.xlCellTypeConstants, _
Value:=Excel.XlSpecialCellsValue.xlNumbers)
On Error GoTo 0
If NewRange Is Nothing Then
MsgBox "Your message here informing the USER that desired cells were not found"
Else
'
'~~> Do whatever you want with the range
'
End If
This is my code:
Sub VerticalLoop()
Dim i As Integer
Range("VerticalLoop").Activate
i = 1
Do While i <= Range("VerticalLoop").Rows.Count
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop
End Sub
For some reason I can't run this. When I press F8, the error pops up when I reach the line "i=1", so I don't know exactly what I did wrong.
This code will fail if the Sheet containing the Defined Name has not been activated first!
The error is in the line
Range("VerticalLoop").Activate
The error is probably a typo in the name of the range. Are you sure it isn't supposed to be "Vertical_Loop" (with the underscore)?
On the other hand, Gary's Student is correct that a '1004' error could be triggered by trying to activate a range in an inactive sheet. The error message itself (and not just the error number) can tell you more about the exact source of the error. Alternatively, you could break that line into two lines like thus:
Dim VLoop As Range
Set VLoop = Range("VericalLoop")
VLoop.Activate
If the Set VLoop line throws the error then the problem is with your name. If the second line throws the error then then problem is with what is active.
As an added benefit -- once you debug this error you now have a useful range variable allowing you to use expressions like VLoop.Rows.Count
Trying to activate the cell in my column A which says "Generator loading".
I have been trying the 'With' and 'End With' commands and other suggested formats posted on the net. However, I keep getting the same error- Run-time Error 91.
From my various trials I am very sure that there is something wrong within the "Find" command, but I cannot figure out what... I have been filling it using the format on the MSDN page.
Do you have any suggestions?
Dim findstring As String
findstring = "Generator loading"
Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1)).Activate
The error commonly arises if the result of the .Find method is Nothing, because you can't do Nothing.Activate
First, you have to check for Nothingness
Dim rng as Range
Set rng = Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1))
If rng Is Nothing Then
MsgBox findString & " not found!!", vbCritical
Exit Sub
End If
rng.Activate
'the rest of your code goes here...
I don't like to work with columns, so instead of it, I used a range.
For me to work, I just replaced the reference of the sheet
Sheets(1).Range("A:A").Find(What:=findstring, After:=Cells(9, 1)).Activate
See if this works. If not, you could send me the sheet if you want :)
I've seen a few posts on this problem. I know what's causing it but I'm trying to figure a way to stop it from appearing.
What it is there is one excel file called Main which I have to keep open and it updates automatically using every 20 mins using:
Application.OnTime
In the VBA formula there is a call for
Loc = Range("location").Value
So if I have another excel file open I get the range error when it auto-updates.
Is there something I can put in before to check that the value isn't right and ignore that one error? I've tried a few things and looked around but nothing really works.
Thanks for any help.
I know what the problem is, I'm trying to find a way to block or check for an empty value while in another file.... Demas 5 mins ago
I agree with Tim here. If you can fully qualify your range then do that. Else read on :)
Is this what you want?
Dim rng As Range
'
'~~> Rest of the code
'
On Error Resume Next
Set rng = Range("location")
On Error GoTo 0
If Not rng Is Nothing Then
Loc = Range("location").Value
'
'~~> Rest of the code
'
End If
hey, first thanks to all for answering my other questions. I am extremely new to Excel VBA and some things I just get hung up on. I have a userform (not embedded in a worksheet) and I have a few fields that are for currency (amounts, etc) and if someone inputs a letter it errors after they hit the command button and they lose all info. I need error code to where I can tell them in a msgbox that they should not put characters in a currency field. I don't need it specific to those fields but I don't want them to lose there data when they hit the command button to dump the data into a spreadsheet.
How can I have them see the error msg, hit the ok button and have it take me right back to the screen without losing the data they have alread entered? Basically give them the opporunity to correct their error but not have to reinput 50 fields?
Thanks
Can't be specific without the actual code, but add error handlers to your code:
Sub SomeRoutine()
Dim stuff
On Error GoTo EH
' Code
Exit Sub
EH:
' Any errors with come here
If Err.Number = <specific errors to trap> Then
MsgBox "Oops..."
'As a debug tools, put a Resume here,
' but be sure to put a break on it,
' and don't leav it in the finished code
Resume
End If
End Sub
As I understand it you want the user to enter numeric numbers only into the text box - right? This is what I normally do.
In a global module add the following function:
Function IFF(c, t, f)
Dim v
If c Then v = t Else v = f
IFF = v
End Function
Then in your textbox_change event add the below:
Private Sub txtAmount_Change()
txtAmount.Text = IFF(IsNumeric(txtAmounto.Text), Val(txtAmount.Text), 0)
End Sub
This will basically put 0 in the box as soon as the user enters an invalid number.
Hope this helps
A slightly different take on error handlers than that given by chris neilsen
Sub SomeRoutine
On Error GoTo ErrHandler 'doesn't matter where you put it
'as long as it's before the code you want to protect
'Dim Stuff
'Do Stuff
ExitRoutine: 'Note the colon(:), which makes this a label
'Any cleanup that you _always_ want
Exit Sub
ErrHandler:
Select Case Err.Number
Case <some error you want to handle specially>
'special handling
Case Else
'default handling, which may include:
Resume ExitRoutine
End Select
Resume
End Sub
Note that that last Resume will never get hit in normal processing (if you've written your error-handling Cases correctly), but will let you set it as "Next Statement" when you're debugging in Break mode. This is an easy way to see exactly which statement threw the error.