delete entire in multiple sheets that have same id

yazan

New Member
Joined
Aug 1, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello Kings,
i have multiple sheets, the first column in each one is "ID number"
i am using this code to delete the entire row in the first sheet
answer = MsgBox("are you sure you want to delete", vbYesNo, vbQuestion)
If answer = vbYes Then
Selection.EntireRow.Delete
Else
'do nothing
End If

but i want if i said yes it will delete the entire rows in everysheet that sharing the same "ID number" ( selection)
please help
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Here you go. Please test on a copy of your workbook.

VBA Code:
Sub DeleteIDAllSheets()
    Dim ans As VbMsgBoxResult
    Dim id As String
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim srRng As Range, srRes As Range
    
    Set wb = ThisWorkbook
    Set ws = ActiveSheet
    
    ans = MsgBox("are you sure you want to delete", vbYesNo, vbQuestion)
    
    If ans = vbYes Then
        id = Selection.Value
        If id <> "" Then
        Selection.EntireRow.Delete
            For Each ws In wb.Worksheets
                With ws
                    Set srRng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
                    Do
                        On Error Resume Next
                        Set srRes = srRng.Find(id, , xlValues, xlWhole)
                        On Error GoTo 0
                        If Not srRes Is Nothing Then
                            srRes.EntireRow.Delete
                        End If
                    Loop While Not srRes Is Nothing
                End With
            Next ws
        Else
            MsgBox "No id selected. Halting execution.", vbOKOnly
        End If
        
    End If
End Sub
 
Upvote 0
Solution
Here you go. Please test on a copy of your workbook.

VBA Code:
Sub DeleteIDAllSheets()
    Dim ans As VbMsgBoxResult
    Dim id As String
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim srRng As Range, srRes As Range
   
    Set wb = ThisWorkbook
    Set ws = ActiveSheet
   
    ans = MsgBox("are you sure you want to delete", vbYesNo, vbQuestion)
   
    If ans = vbYes Then
        id = Selection.Value
        If id <> "" Then
        Selection.EntireRow.Delete
            For Each ws In wb.Worksheets
                With ws
                    Set srRng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
                    Do
                        On Error Resume Next
                        Set srRes = srRng.Find(id, , xlValues, xlWhole)
                        On Error GoTo 0
                        If Not srRes Is Nothing Then
                            srRes.EntireRow.Delete
                        End If
                    Loop While Not srRes Is Nothing
                End With
            Next ws
        Else
            MsgBox "No id selected. Halting execution.", vbOKOnly
        End If
       
    End If
End Sub
i do not how to thank you .. you are awesome ! thank you very much
 
Upvote 0

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

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