0

I need to delete specific rows in a table with a order number. When I put this code in Excel it delete the entire table. What I want is to delete only specific rows. The tbxOrder is a text box. I want that when the text box equals x command number the code will select all the rows with the same number in the "Pagination" table (column 20) and then delete them. Thank you :)

Public Sub DeleteOrderRows()

Dim strNoOrder As String
Dim strNoFunction As String
Dim noCommande As Integer

Dim tblPagination As ListObject
Set tblPagination = Worksheets("Pagination").ListObjects.Item("tblPagination")

For Each srcrow In tblPagination.ListRows

strNoOrder = srcrow.Range.Cells(1, 20)
noOrder = tbxOrder.Value

If strNoOrder = noOrder Then

EntireRow.Delete

End If

Next

End Sub
4
  • Is that all of your code? You're missing a Next srcrow and an End If... Also, when you do srcrow.Range.Cells(1,20) (if that .Range.Cells() works), you're referring to the relative Cells(1,20) within srcRow. Is that what you mean? Are you not getting any error?? Commented Aug 16, 2017 at 20:07
  • Yes Excel VBA Run-time error '424': Object Required at EntireRow.Delete. Commented Aug 16, 2017 at 20:13
  • This might be as easy as srcrow.EntireRow.Delete. Commented Aug 16, 2017 at 20:17
  • The same Run-time error appears... Commented Aug 16, 2017 at 20:22

2 Answers 2

1

Try it without the ListRows; use DataBodyRange instead. Work from the bottom to the top or you risk skipping rows when you delete.

Option Explicit

Public Sub DeleteOrderRows()

    Dim strNoOrder As String
    Dim strNoFunction As String, noOrder As String
    Dim noCommande As Integer
    Dim i As Long, tbxOrder As Range
    Dim tblPagination As ListObject

    Set tblPagination = Worksheets("Pagination").ListObjects.Item("tblPagination")

    'here I had to set tbvOrder and assign noOrder
    Set tbxOrder = Worksheets("Pagination").Cells(1, "A")
    noOrder = tbxOrder.Value

    With tblPagination.DataBodyRange.Columns(20).Cells
        For i = .Count To 1 Step -1
            Debug.Print .Cells(i).Address(0, 0)
            strNoOrder = .Cells(i).Value2

            If strNoOrder = noOrder Then
                .Cells(i).EntireRow.Delete
            End If

        Next i
    End With

End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

An other option :

Public Sub DeleteOrderRows()
Dim rngToDelete As Range
Set rngToDelete = Nothing

Dim tblPagination As ListObject
Set tblPagination = Worksheets("Pagination").ListObjects.Item("tblPagination")

Dim strNopage As String
Dim strNoOrder As String

For Each currentRow In tblPagination.ListRows
    strNoOrder = currentRow.Range.Cells(1, 5).Value
    strNopage = tbxPage.Value
    If strNoCommande = strNopage Then
        If rngToDelete Is Nothing Then
            Set rngToDelete = currentRow.Range
        Else
            Set rngToDelete = Union(rngToDelete, currentRow.Range)
        End If
    End If
Next

If Not rngToDelete Is Nothing Then
    rngToDelete.Delete Shift:=xlUp
End If

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.