I have taken over someone else's macro who has left the organisation. I get the following error as listed in the title with Run time error.
Below is the code it is telling me to debug. Problem is this is my first time to VBA macro's and I am unsure where to begin to solve the error.
Any help would be great as I can't go past this point.
Cells.Find(What:="Top 10 Rank", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:= xlNext, MatchCase:=False, _
SearchFormat:=False).Activate
If the value is not found in the search range then you will get an error, so it's best to split up your code into separate operations:
Dim f As Range
Set f = Cells.Find(What:="Top 10 Rank", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:= xlNext, MatchCase:=False, _
SearchFormat:=False)
If Not f Is nothing Then
'do somthing with f
Else
Msgbox "not found!"
End If
No data was found, so .Find returned a null object reference (Nothing in VBA), as explained in this Microsoft Support page:
This macro error occurs because the Visual Basic Find method returns a NULL value which makes activating a cell impossible.
Related
I need to reverse engineer someone else's code and make it run. Right now it is throwing an error at me complaining that some variable isn't set.
I'm not sure what it is talking about exactly. Here is the top part of the subroutine with the error.
Sub OBI_Vendor_Detail_Macro()
Cells.Select
With Selection
.WrapText = False
.MergeCells = False
End With
Range("C4").Select
Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
The last line produces this error Run-time error '91': Object variable or With block variable not set
Hope I've provided enough info. If not please let me know. Thanks.
You need to try to Find first, then check that you found something:
Dim foundCell As Range
Set foundCell = Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not foundCell Is Nothing Then
foundCell.Activate
End If
I just wrote a seven page macro with two UserForms that is a thing of beauty.
There are two data bases and the one on the left is longer than the one on the right.
I use a variable DDataa1 and the VBA function:
Sub SSearchh()
... code ...
Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
... code ...
End Sub
It works perfectly EXCEPT when there is no DDataa1 to find. (That is, there is no variable in the right hand list that is being searched for in the left hand list.)
In that case the Macro just stops. I want to capture this "data not found" event and write more code specific to this failure but this function does not seem to generate a True/False condition.
Specific assistance would be appreciated
You could try
Dim FindRange As Range
Set FindRange = Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If FindRange Is Nothing Then
' do error handling here
Else
FindRange.Activate
End If
I am trying to search for the date, which I have in cell K1 as =today()....
Whenever I am recording the macro, however, it will continue to use the date which the macro was written. What I am curious about is a methodology of pasting what is on the clipboard into the VBA search function.
So I want to search column A for what is in cell K1. This is what code I have now (the find what is what I just typed to help you all have an idea of what I'm looking for)
Range("K1").Select
Selection.Copy
Columns("A:A").Select
Selection.Find(What:="COPY CONTENTS OF CELL K1", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
This should do it. Note that your original code will throw an error if today's date isn't found. Typically when working with Find() it's safer to test the returned value before trying to do something with it.
Dim f As Range
Set f = Columns("A:A").Find(What:=Date(), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing then
'do something with f
Else
'???? do what
End If
You could replace your entire code with this:
Activesheet.Columns("A:A").Find(What:=ActiveSheet.Range("K1"), After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
What was happening is that there's no way to paste a cell's contents via .Copy directly into a VBA function. So I placed what you wanted to paste into it into the function directly.
I think you will also find this helpful:
How to avoid using Select in Excel VBA macros
This is a sample of what I'm putting on my macro:
Cells.Find(What:="M104", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireRow.Delete
If excel can't find "M104", then it gives you a error message and the macro won't run.
I tried "On Error Resume Next" before the coding above and it ignored the find error, but deleted the row. Is there a way to make it ignore both the find function and the row deletion?
Thanks for the help!
On Error Resume Next does exactly what it says it does: resumes execution of code at the next line.
The error arises because you've piggy-backed the Activate on to a method that might return Nothing.
Here is a very crude way of handling this error:
On Error Resume Next
Cells.Find(What:="M104", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'If there was not an error, then delete the row
If Err.Number = 0 Then
Selection.EntireRow.Delete
End If
'Resume the default error handling
On Error GoTo 0
Here is a more sophisticated way. First, declare a Range object to hold the result of the Find.
Dim foundRange as Range
Set foundRange = Cells.Find(What:="M104", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not foundRange Is Nothing Then foundRange.EntireRow.Delete
In the second example, I have programmed to anticipate the error, and built some logic to handle the potential case of "not found".
You can also use error handling GoTo blocks with Resume _line_ option, but that's generally more cumbersome and I try to avoid that where possible
This question already has answers here:
how to detect whether VBA excel found something?
(3 answers)
Closed 7 years ago.
I receiving a strange error when running this subroutine in VBA:
Sub NameColumns()
' name key columns for later reference in formulas
Dim startdatecol As Integer
' name start date column
startdatecol = ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
End Sub
Run time error '91': Object variable or With variable not set
Any ideas on how I can fix this subroutine error? And why it is occurring?
Thanks,
AME
The problem is that Find is not finding the cell.
You will find (pun intended) that the following is true:
MsgBox ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) Is Nothing
The first thing you should do is fix your search so that it finds the cell you're looking for.
Edit:
Maybe a change that would better illustrate the problem is this:
Dim found as Range
Dim startDateCol as Integer
Set found = ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not found Is Nothing Then startDateCol = found.Column
MsgBox startDateCol 'This will be zero if the "Start Date" cell wasn't found.
Edit to respond to comment:
'This should find the exact text "Start Date" (case sensitive) in the header row.
'A cell containing, for example "The Start Date" will not be matched.
Set found = ActiveSheet.Range("1:1").Find("Start Date", LookIn:=xlValues, _
LookAt:=xlWhole, MatchCase:=True)