How to delete row on two worksheets

jdcar

New Member
Joined
Oct 9, 2021
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone

I have a spreadsheet (sheet1) with several general article data, and a second spreadsheet (sheet2) that specifies one of them, with a different record order.
I would like to use a command in the userform, so that when I wanted to delete a certain record, on sheet 1, it would be deleted on the line of the corresponding sheet on sheet 2.
The difference between the record on sheet1 and sheet2 is only the entry order number, the rest is the same.
My thanks for anyone who can help me.
What I use is below:
VBA Code:
Private Sub CmdB1_Click()    'DELETE     
                                                
    Dim i, ip As Long
    Dim Ws As Worksheet
    Dim r As Long
    Dim NextRow, RowUlt As Long
    Dim linha As Integer

 Application.ScreenUpdating = False

    On Error Resume Next
        'delete data,without reference to products
      Set Ws = Sheets("imputdata")
     
NextRow = Ws.Range("b" & Rows.Count).End(xlUp).Row + 5

    If ListBox1.Value = "" Then
        MsgBox ("Please fill up, the Months or Value commands, and select them")
    End If

    With ListBox1
        If MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Delete row") = vbNo Then
            Exit Sub
          Else
           For i = 1 To Sheet1.Range("b10000").End(xlUp).Row
             If Sheet1.Cells(i, "B") = Val(Me.ListBox1.Column(0)) Then
                  Sheet1.Range("B" & i).EntireRow.Delete
                  ListBox2.Clear
             End If
            Next i

           For ip = 1 To Sheet2.Range("b10000").End(xlUp).Row
             If Sheet2.Cells(ip, "B") = Val(Me.ListBox1.Column(0)) Then
                  Sheet2.Range("B" & ip).EntireRow.Delete
                  ListBox2.Clear
             End If
            Next ip
 end with
end sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this code

VBA Code:
Private Sub CmdB1_Click() ' DELETE
    Dim i As Long
    Dim ip As Long
    Dim Ws As Worksheet
    Dim NextRow As Long
    Dim linha As Integer


    Application.ScreenUpdating = False


    On Error Resume Next
    ' Delete data without reference to products
    Set Ws = ThisWorkbook.Sheets("imputdata")


    NextRow = Ws.Range("B" & Ws.Rows.Count).End(xlUp).Row + 5


    If ListBox1.Value = "" Then
        MsgBox "Please fill up the Months or Value commands and select them."
        Exit Sub
    End If


    With ListBox1
        If MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Delete row") = vbNo Then
            Exit Sub
        Else
            For i = Ws.Range("B10000").End(xlUp).Row To 1 Step -1
                If Ws.Cells(i, "B").Value = Val(Me.ListBox1.Value) Then
                    Ws.Cells(i, "B").EntireRow.Delete
                End If
            Next i
        End If
    End With


    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello Mr Muhammad


Thanks for the quick response.
My code above works very well, when I want to delete a selected line on Sheet1, BUT the problem is that it doesn't delete it on Sheet2.
how resolve this problem
thak you
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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