Don't understand why my Activate function fails in vba - excel

Sub FindMatch()
Dim Cell As Range
Worksheets("Member data").Activate
Columns("T:T").Select
Set Cell = Selection.Find(What:="arabinow#gmail.com", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
MsgBox ("Nothing")
Else
MsgBox ("Something)
End If
End Sub
Keep getting Runtime error 1004
Activate method of worksheet class failed
I definitely have a sheet in my workbook called "Member data"
I ran the sub and expected that sheet to be the active sheet

Try it without using Activate or Select:
Sub FindMatch()
Dim Cell As Range
With Worksheets("Member data")
Set Cell = Columns("T:T").Find(What:="arabinow#gmail.com", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
MsgBox ("Nothing")
Else
Debug.Print Cell.Address
MsgBox ("Something")
End If
End With
End Sub

Related

How to get resolve Run time error 13 in my code for find and delete entire row in excel

I am trying to write a VBA Script to delete a row which is selected from a dropdown list.
My code is
Sub delete_md_entry()
Dim LR As Long
Dim str As Range, rfnd As Range
'LR = Sheets("MASTER_DATA").Range("C2000").End(xlUp).Row
str = Sheets("MASTER_DATA").Range("I2").Value
If Len(str) <> 0 Then
Set rfnd = Selection.Find(str, After:=Range("C5"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rfnd Is Nothing Then
yes = MsgBox("Do you want to delete" & rfnd.Value & " row?", vbYesNo, "Alert!")
If yes = vbYes Then
rfnd.EntireRow.Delete
Else
Exit Sub
End If
Else
MsgBox "NO ROW FOUND"
End If
Else
MsgBox "Kindly select a name!"
End If
End Sub
Unfortunately it is not working and throw an error Run time error 13: Type mismatch.
However there is not with selection.
Where I made the mistake? Please help me to find out the error!
Find works on a Range and not on a single cell Selection.
You need to change
Set rfnd = Selection.Find(str, After:=Range("C5"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
To:
Set rfnd = Range("A2:C10").Find(str, After:=Range("C5"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Of course you need to change Range("A2:C10") above to whatever the range it is you are looking in.

Object variable or With block variable not set error (Find And Replace)

Here Is what i need to do :
First , I have Two sheets ("AM Production","PM Production") need to Find String "Pcs" In the each sheet and count the results then Excute macro multiple times depending on that count in both sheets (Every sheet with its own count) So i did the following : - I have Two Macros one counts pcs word in the sheet and the other excute the Second macro with that number.
Sub FindPcs()
Range("N1").Select
'Find
Cells.Find(What:="Pcs", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'Found Nothing
'Replace
ActiveCell.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Copy To Above Cell
ActiveCell.Range("A1:B1").Select
Selection.Copy
ActiveCell.Offset(-1, 0).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End Sub
The Action Macro :
Sub FindMultipleTimes()
Dim x As Integer
x = "=COUNTIF(C[10],""Pcs"")"
For i = 0 To x
Application.Run "PERSONAL.XLSB!FindPcs"
Next i
End Sub
I need to merge the two macros As The main idea is to find pcs in the "AM Production" sheet then execute Sub FindMultipleTimes() in the end when it find nothing it goes to "PM Production" and Repeat the Counting and Executing part .
Note :I tried the Range and If Nothing Method with find but it throws another error object required.
Thanks in Advance.
No need to call the macro multiple times, use a Do .. Loop Until loop.
Option Explicit
Sub FindMultipleTimes()
Dim sht
For Each sht In Array("AM Production", "PM Production")
FindPcs Sheets(sht)
Next
End Sub
Sub FindPcs(ws As Worksheet)
Dim fnd As Range, n As Long
Application.ScreenUpdating = False
With ws
Set fnd = .Cells.Find(What:="Pcs", After:=.Range("N1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not fnd Is Nothing Then
Do
fnd.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'Copy To Above Cell
fnd.Resize(1, 2).Copy fnd.Offset(-1)
fnd.EntireRow.Delete
n = n + 1
Set fnd = .Cells.FindNext
Loop Until fnd Is Nothing
End If
End With
Application.ScreenUpdating = True
MsgBox n & " found on " & ws.Name
End Sub

VBA Array Formula and Goal Seek in IF Then Statement

This code doesnt run. It first looks into a range, if 0 does not exist then select least negative value, by using array formula. Then goal seek to set selected cell to 0 by changing value of a cell on same row, left 4 columns. If 0 exists do nothing. Any help appreciated
Sub Test()
Dim Cel As Integer
For Each Cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57")
If Cel.Value <> 0 Then
Cel.Find(Application.WorksheetFunction.FormulaArray(MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))))
Cel.Select
Cel.GoalSeek Goal:=0, ChangingCell:=Cel.Offset(0, -4)
End If
Next Cel
End Sub
Looks like the previous comments/responses were deleted. Here's the latest version of the code, modified as per previous responses. Still doesn't run. I removed .Activate at the end of Cel.Find. Now there's a compile Syntax error. Any help apprciated
Sub Test()
Dim Cel As Range
For Each Cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57").Cells
If Cel.Value <> 0 Then
Cel.Find(What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Cel.GoalSeek Goal:=0, ChangingCell:=Cel.Offset(0, -4)
End If
Next Cel
End Sub
Just delete the parentheses in your Find method.
Use:
Cel.Find What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False
Instead of:
Cel.Find(What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

Excel VBA: Using FIND: How to select the cell where the item is found?

I am using Excel VBA to search for a string e.g. "CCC" in an Excel worksheet.
I used the simple code shown.
However, I want VBA to select the cell where the first occurrence of "CCC" is found. (Just like when you do a FIND manually).
How can I modify my code to achieve this?`
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rng Is Nothing Then
MsgBox ("found")
Else
MsgBox ("not found")
End If
End Sub
To select a cell, you can use the Select method. So, if you have a Range object which has been set to the cell you want, you can apply the Select method to that object, i.e.
rng.Select
Incorporated into your existing code, it might look something like:
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rng Is Nothing Then
MsgBox "found"
rng.Select
Else
MsgBox "not found"
End If
End Sub
You can use rng.Select but it needs the worksheet to be active on top (or you habe to activate it first i.e. rng.Parent.Activate). Simplest is to use Application.Goto rng
If Not rng Is Nothing Then
Application.Goto rng
MsgBox ("found")
Else
MsgBox ("Not found")
End If
Private Sub CommandButton1_Click()
Range("A1").select
Cells.Find(What:="CCC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).activate
activcecell.select
If Not rng Is Nothing Then
MsgBox ("found")
Else
MsgBox ("not found")
End If
End Sub
Using activecell.select will select your first find cell.

Seach and find value based on value in another cell

I am trying to build a macro that will search a specific column.
Here are the steps:
1. user enters a number into the cell and then executes the macro.
2. based on the value of what the user has entered, the macro will find the text in a column.
I got everything to work pretty well except I don't know how to define the value of the cell that the user enters. Any help here would be appreciated.
Sheets("New Version ").Select
Range("B4").Select
Sheets("PN_List").Select
Columns("I:I").Select
'below is where I struggle
Selection.Find(What:=(""), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Let's say the user enters a number into cell B4, then you just have to adjust your code into:
Selection.Find(What:=Range("B4").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
You can do this in 2 ways.
Number1:
Module based: (code in module)
Sub Sample()
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
Number2:
Worksheet Event based. (code in Sheet)
Private Sub Worksheet_Change(ByVal Target as Range)
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
If Not Intersect(Target, search_value) Is Nothing Then
query = Msgbox("Search data?", vbYesNo)
If query = 7 Then Exit Sub
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
The first one you enter data in B4 then run the macro.
The second one fires every time you change value in B4.
A msgbox will appear asking if you want to search the data entered.
Hope this helps.

Resources