Excel macro search for word on sheet - vba

This is an easy one, but I can't get it to work. I need to search for the word "Tons" on a sheet.
Sub findText()
If Cells.Find(What:="Tons", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate = True Then
MsgBox "Word Found"
Else
MsgBox "Word Not Found"
End If
End Sub
I get the error "Object variable or With Block variable not set". My code works whenever the word "Tons" is on a sheet but comes back with an error whenever it is not.
Thanks for the help.

Consider:
Sub findText()
Dim r As Range
Set r = Cells.Find(What:="Tons", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not r Is Nothing Then
MsgBox "Word Found"
Else
MsgBox "Word Not Found"
End If
End Sub
Try to create a Range and then test if you succeeded.

Related

Why does it come out variable not set?

I have a problem, when I try to debug this coding it will appear
RUNTIME ERROR 91; OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET
and it also will highlight at this line
Range("L8").Value = Cells(2, FindMe.Column).
May I know what is the error?
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
Set Searchme = Range("L9")
Set FindMe = Range("A2:G126").Find(What:="Searchme", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
Range("L8").Value = Cells(2, FindMe.Column)
Range("A1:G126").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"L8:L9"), CopyToRange:=Range("N8:T8"), Unique:=False
End Sub
The value that you're trying to find doesn't exist in the range.
Try to wrap your code around If condition to check if find returns anything or not !
Sub test()
Set Searchme = Range("L9")
Set FindMe = Range("A2:G126").Find(What:="Searchme", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If FindMe Is Nothing Then
MsgBox "No value found."
'or you can negate the condition like "If Not" if you want to continue and remove the else part.
Else
Range("L8").Value = Cells(2, FindMe.Column)
Range("A1:G126").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"L8:L9"), CopyToRange:=Range("N8:T8"), Unique:=False
End If
End Sub

vba Entire Column should copy

" find " cell value in header will keep changing in raw file, i need " find " cell value ENTIRE column should copy and paste in sheet2
Sub Macro3()
Cells.Find(What:="FSP Center", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate`
Cells.FindNext(After:=ActiveCell).Activate`
Columns("A:A").Select 'i want to select entire column
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
End Sub
Sub Macro3()
Dim f As Range
Set f = Rows(1).Find(What:="FSP Center", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not f Is Nothing Then
f.EntireColumn.Copy Sheets("Sheet2").Range("A1")
End If
End Sub
'fixed the misspelling "If"

VBA - how to go about the automation of checking the inputs (newbie here)

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

If (search term) found, do (action). If not, end if

If you guys could help me out, that would be great because it would really help me.
Here's what I'm trying to do:
Search for a cell with a specific term
If found, copy the entire row that the cell is in and paste it into a row above it.
If not found, do nothing and continue with the code
Here's my code:
Sub Test()
'
' Test Macro
'
' Keyboard Shortcut: Ctrl+b
'
Range("A5").Select
Cells.Find(What:="PL 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
If Not IsEmpty(ActiveCell.Value) Then
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A5").Select
ActiveSheet.Paste
End If
Range("A5").Select
Cells.Find(What:="PL 2", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
If Not IsEmpty(ActiveCell.Value) Then
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A6").Select
ActiveSheet.Paste
End If
Range("A5").Select
Cells.Find(What:="PL 3", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
If Not IsEmpty(ActiveCell.Value) Then
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A7").Select
ActiveSheet.Paste
End If
End Sub
My code only works if the value is found. If it's not found it runs into the error below:
Cells.Find is a function that returns a Range object reference; when it doesn't find anything, the reference will be Nothing. And you can't call .Activate on Nothing:
This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell. (MSDN)
Cells.Find(What:="PL 2", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
You need to rewrite your code and avoid .Select and .Activate, and avoid working with ActiveCell and implicitly with ActiveSheet (which you are doing by not qualifying the Cells call with a proper worksheet reference).
Your formatting makes it hard to read the code, for several reasons:
Arguments are being specified on different lines
Line continuations are being palced at arbitrary locations
Nested member calls aren't lined up
Compare to:
Cells.Find(What:="PL 2", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) _
.Activate
That's just readability. The problem is that you basically assume that .Find returns a valid object reference. Don't assume, explicitly check:
Set result = Cells.Find(...)
If Not result Is Nothing Then result.Activate
But really, you need to figure out a way to avoid .Select and .Activate.
You can try something like this instead (untested):
Sub HideAndSeek()
Dim foundCell As Range
For i = 1 To 3
Set foundCell = Cells.Find(What:="PL " & i, LookIn:=xlFormulas, LookAt:=xlPart)
If Not foundCell Is Nothing Then
Intersect(foundCell.EntireRow, ActiveSheet.UsedRange).Offset(-1, 0).Value = _
Intersect(foundCell.EntireRow, ActiveSheet.UsedRange).Value
End If
Set foundCell = Nothing
Next
End Sub
The principle being that you write the code you need once and then create a loop to repeat the code for you.
The other part of this answer is checking that the cell was found - to do this we check that the range was actually set (which means it isn't Nothing) using
If Not foundRange Is Nothing

Error when setting some sheets in code but not with others

I have a large macro for automating invoices for my company. Sometimes we have proforma invoices that mean we delete two of the three invoices that are manipulated (Client, Owner, and VAT). Therefore I have to check before each code is run whether the sheet exists.
The problem I've run into is that it will give me a run time error 424 when I set some of the sheets to be checked. In the code below it is the second time that the TVA sheet is checked for where the error occurs (If TVASheet Is Nothing Then). Mind you I have almost exactly the same code running above it except it is checking for ClientSheet instead.
'If the current payment is the only payment then we add to the TVA invoice
'that the rest is due
'on the date the balance of rental is due
If (Range("F3") <> "" And Range("G3") = "" And Range("H3") = "") Then
On Error Resume Next
Set TVASheet = Sheets("TVA Invoice")
On Error GoTo 0
If TVASheet Is Nothing Then
Else
Sheets("TVA Invoice").Select
If Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then
Cells.Find(What:="Select!F3", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Font.Bold = False
ActiveCell = _
"=100 - 100*TEXT(Select!F3,""0%"")&""% du paiement sera reçu le ""&PROPER(TEXT(Select!C30,""JJ-MMMM-AAAA""))&"""""
Else
End If
End If
Else
On Error Resume Next
Set TVASheet = Sheets("TVA Invoice")
On Error GoTo 0
If TVASheet Is Nothing Then
Else
Sheets("TVA Invoice").Select
If Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then
Else
Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp
End If
End If
End If
Sheets("Select").Select
Declare your TVASheet variable at the beginning of your code:
Dim TVASheet As Worksheet
Obviously the "TVA Invoice" sheet is not found and the Set command fails. Because of your On Error Resume Next the program continues and the interpreter has no clue what TVASheet should be in your If clause. You can avoid this by the declaration.