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)
Related
I'm trying to set up a code that will find a cell using the find function and then select a different cell in that column. I store the column number as a variable, and then try to move to a cell in the same column using the variable, but its not working.
I've tried changing it so that the column is stored as a string instead of an integer and tried using the .Cells method instead, neither have worked.
Dim numCol As String
Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=
_
False, SearchFormat:=False).Activate
numCol = ActiveCell.Column
Range(numCol & "4").Select
Getting a 1004 "method range of object global failed" error from that last line of code.
Here's some modified code from your query that might be useful. You want to try to use the properties of the range. In the below example I've defined a range that is the found cell fcell. A couple examples of what you can do ar there.
Dim numCol As Long
Dim fcell As Range
'this will find the cell and set it as a variable of fcell
Set fcell = Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'you can select it
fcell.Select
'you can select the whole column
fcell.EntireColumn.Select
'you can select the column number (if one column)
numCol = fcell.Column
'you can return the address
MsgBox "the address is " & fcell.Address
Good luck.
Im trying to make excel search for a text string in a specific column in a specific worksheet that is not the active worksheet. VBA gives me an error that says i cannot use this method of selection. so my question is, do you have a suggestion to do it in another way?
Worksheets("Parts for renovation").Columns("Q:Q").Select
Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Worksheets("Parts for Renovation").ActiveCell.Offset(0, 4)
End If
There is no need to select anything there:
With Worksheets("Parts for renovation").Columns("Q:Q")
Set cell = .Find(What:="Total transfer price", After:=.Cells(1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Cell.Offset(0, 4)
End If
End With
Your error comes, because you select a range on a non-active worksheet. This is one of the reasons, why you should be avoiding select in general.
However, if you want to make your code working (which is strongly not advisable), you may consider selecting the worksheet before selecting the range:
Worksheets("Parts for renovation").Select
Columns("Q:Q").Select
For the advisable part, try to avoid the usage of "Select" -
How to avoid using Select in Excel VBA
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.
Specifically what I would want to do is find a way to check if a certain input in Sheet1 cell, is also found in sheet2.
Not knowledgeable with VBA so I tried recording macro
Sheets("Sheet2").Select
Cells.Find(What:="asd", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Now instead of string "asd", I want the input in sheet1, below
Sheets("Sheet1").Select
Range("B1").Select
I tried changing "asd" to input in sheet1,
Sheets("Sheet2").Select
Cells.Find(What:=
Sheets("Sheet1").Select
Range("B1").Select, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
but it's giving me an error. Any one can please help how to go about this or recommend a different approach to resolve my problem.
If you want to compare strings in different sheets, just create a new button and add fallowing code:
If Sheets("sheet1").Range("b1") = Sheets("sheet2").Range("b1") then
msgbox " string match"
Else
msgbox " string don't match"
End If
This code will compare cell b1 from sheet1 to cell b1 from sheet2
Try this:
Cells.Find(What:=ThisWorkbook.Sheets("Sheet1").Range("B1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False).Select
EDIT: As I told you in the comment
Dim MyCell as Range
Set MyCell = ThisWorkbook.Sheets("Sheet2").Cells.Find(What:=ThisWorkbook.Sheets("Sheet1").Range("B1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
'This will check if it is found or not
If MyCell Is Nothing Then
MsgBox "Did not find it"
Else
MsgBox "Found it"
End If
Use the Instr function
Dim pos As Integer
pos = InStr(Sheets("sheet2").Range("b1"), Sheets("sheet1").Range("b1"))
If pos<> 0 then
msgbox " string match"
Else
msgbox " string don't match"
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