Macro to find a cell value, across multiple sheets and then delete the row containing the value

LauraC1984

Board Regular
Joined
Jan 30, 2020
Messages
63
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am currently working on a macro that will take the value from cell B12 on sheet name 'Input', It searches many sheets and when the value is found it deletes the row. I have the following macro but it keeps debugging at different points. and I don't understand the errors
can anyone help?

Sub updateJobdelete()
Dim fn As Range, i As Long, sh As Worksheet, adr As String, cnt As Long
Set sh = Sheets("Input")
chg = True
Dim ssh As Worksheet
For Each ssh In ThisWorkbook.Sheets
If ssh.Name <> "Input" And ssh.Name <> "Calendar" And _
ssh.Name <> "List" And ssh.Name <> "2020" Then
ssh.Visible = True
Set fn = ssh.Range("C:C").Find(sh.Range("B12").Value, , xlValues, xlWhole)
If Not fn Is Nothing Then
adr = fn.Address
Do
For i = ("B12") To 1 Step -1
If Range("C" & i).Value = Target.Value Then
Rows(i).delete
ct = ct + 1
End If
If fn Is Nothing Then
cnt = cnt + 1
End If
ssh.Visible = False
Next sheets("Calendar").Select
ActiveWorkbook.Save
If cnt = i - 2 Then
MsgBox "Job Number for job delete not found", vbExclamation, "OOPS"
Else
MsgBox "Change processed successfully", vbInformation, "DONE"
End If
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Im afraid i cant see how that code works. I can see a few problems. Explain what you are trying to do. You have a value in a cell and you want to check other sheets for that value? Could the value be a date? They can cause issues. Will/could the value appear more than once in a sheet?
 
Upvote 0
Hi sorry, the code is a mix of a few macros I have, I was trying to do it myself but it hasn't worked. so maybe best to start from scratch.
ok I want the macro to search for a value that will be in cell B12, It needs to ignore a few sheets which are noted at the beginning of the macro, I think its this section
For Each ssh In ThisWorkbook.Sheets
If ssh.Name <> "Input" And ssh.Name <> "Calendar" And _
ssh.Name <> "List" And ssh.Name <> "2020" Then
ssh.Visible = True

The value can be found in these sheets but I don't want the macro to do anything with them.

the value shouldn't be found more than once in any other sheets, and should be found in column C of the worksheet.
Once the value is found, I want it to delete the row in which the value is found.
Then I want it to go back to the original input screen and delete the original values
all sheets that are to be searched are hidden and need to stay hidden

does this help?
 
Upvote 0
What sort of value is in B12?
Also, when you say
Then I want it to go back to the original input screen and delete the original values
Do you mean to delete the contents of B12?
 
Upvote 0
See if something like this does the deletions as required.

VBA Code:
Dim myFind As String, myVal As Range, shArr

myFind = Sheets("Input").Range("B12").Value
shArr = Array("Input", "Calendar", "List", "2020")

For Each sh In ThisWorkbook.Worksheets
    If Not IsNumeric(Application.Match(sh.Name, shArr, 0)) Then
        Set myVal = sh.Range("C:C").Find(myFind, , xlValues, xlWhole)
        If Not myVal Is Nothing Then
            sh.Rows(myVal.Row).Delete Shift:=xlUp
        End If
    End If
Next
 
Upvote 0
See if something like this does the deletions as required.

VBA Code:
Dim myFind As String, myVal As Range, shArr

myFind = Sheets("Input").Range("B12").Value
shArr = Array("Input", "Calendar", "List", "2020")

For Each sh In ThisWorkbook.Worksheets
    If Not IsNumeric(Application.Match(sh.Name, shArr, 0)) Then
        Set myVal = sh.Range("C:C").Find(myFind, , xlValues, xlWhole)
        If Not myVal Is Nothing Then
            sh.Rows(myVal.Row).Delete Shift:=xlUp
        End If
    End If
Next

thanks Steve sorry still new to all this - do I put my find value between the two , , next to my find?
 
Upvote 0
How about
VBA Code:
Sub LauraC()
   Dim Ws As Worksheet
   Dim Fnd As Range
   Dim Crit As Double
   
   Crit = Sheets("Input").Range("B12").Value
   For Each Ws In Worksheets
      Select Case Ws.Name
         Case "Input", "Calendar", "List", "2020"
         Case Else
         Set Fnd = Ws.Range("C:C").Find(Crit, , xlValues, xlWhole, , , False, , False)
         If Not Fnd Is Nothing Then Fnd.EntireRow.Delete
      End Select
   Next Ws
   Sheets("Input").Range("B12").ClearContents
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top