vba to delete table row

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
116
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,

I have a file where I have an inputbox, where the user types "MyInputMatriculaMaiusculas" values.
This variable is then used to o through a table and delete the row, where this value is in the second column.

Or, at least, that was the goal, because, in reality, this code is doing nothing..
can anybody help me?

This code is suposed to go to "Tabelas Apoio" sheet, and go to "Viaturas" table, look for the number plate in the second column of said table and then, delete that same row.

What am I doing wrong here?

VBA Code:
Dim tblA As ListObject
Dim a As Long

Set tblA = Worksheets("Tabelas Apoio").ListObjects("Viaturas")
For a = tblA.ListRows.Count To 1 Step -1
  If tblA.DataBodyRange(a, 2) = MyInputMatriculaMaiusculas Then
    tblA.ListRows(a).Delete
  End If
Next a


First tried this solution, but it didn't work too:

VBA Code:
For a = Worksheets("Tabelas Apoio").ListObjects("Viaturas").ListRows.Count To 1 Step -1
  If Cells(a, 2).Value = MyInputMatriculaMaiusculas Then
    Cells(a, 2).EntireRow.Delete
  End If
Next a
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
with this data....
1721212768631.png

To iterate from the bottom to the top and delete each row where the PID value is 2, run this

try this

Sub DelLine()
Dim ws As Worksheet
Dim lo As ListObject
Dim r As ListRow
Dim lCounter As Long

Set ws = ThisWorkbook.Sheets("Sheet3") 'sheetname is imaginatively titles Sheet3

Set lo = ws.ListObjects(1) 'it's the only listobject on the tab

For lCounter = lo.DataBodyRange.Rows.Count To 1 Step -1
Set r = lo.ListRows(lCounter)
If r.Range(1, 1) = 2 Then
r.Delete
End If

Next lCounter

End Sub
 
Upvote 0
Solution
with this data....
View attachment 114180
To iterate from the bottom to the top and delete each row where the PID value is 2, run this

try this

Sub DelLine()
Dim ws As Worksheet
Dim lo As ListObject
Dim r As ListRow
Dim lCounter As Long

Set ws = ThisWorkbook.Sheets("Sheet3") 'sheetname is imaginatively titles Sheet3

Set lo = ws.ListObjects(1) 'it's the only listobject on the tab

For lCounter = lo.DataBodyRange.Rows.Count To 1 Step -1
Set r = lo.ListRows(lCounter)
If r.Range(1, 1) = 2 Then
r.Delete
End If

Next lCounter

End Sub
Adapted your code to my reality, and at least it deleted a row on the table, but it was the wrong one.

Here it is my code:

VBA Code:
Dim tblA As ListObject
Dim a As Long
Dim rA As ListRow

Set tblA = Worksheets("Tabelas Apoio").ListObjects("Viaturas")
    For a = tblA.DataBodyRange.Rows.Count To 1 Step -1
        Set rA = tblA.ListRows(a)
        If rA.Range(a, 2) = MyInputMatriculaMaiusculas Then
            rA.Delete
        End If
    Next a

Here is part of my data:
(I'm trying to delete an entire row, when the value in column G (second column of my table) is the same that I'm looking for)

km veiculos_V6.xlsm
FGH
6C. CustosMatriculaDescrição
7001-01250-68-TJFORD TRANSIT
8001-02511-LJ-08RENAULT CLIO
9001-02784-LM-03ISUZU
Tabelas Apoio
 
Upvote 0
Your code in Post#1 worked fine for me.
In your latest code in Post#3, change the if statement to this:
rA is already using the row reference and you now want the same row ie 1 but the 2nd column.
Rich (BB code):
        If rA.Range(1, 2) = MyInputMatriculaMaiusculas Then
 
Upvote 0
Your code in Post#1 worked fine for me.
In your latest code in Post#3, change the if statement to this:
rA is already using the row reference and you now want the same row ie 1 but the 2nd column.
Rich (BB code):
        If rA.Range(1, 2) = MyInputMatriculaMaiusculas Then
Yeap, that worked.

Still dont know why my code from post#1 didn't do anything, but it's working now.

Thank you @Rovex and @Alex Blakenburg
 
Upvote 0
Just an FYI, this is case sensitive:
If rA.Range(1, 2) = MyInputMatriculaMaiusculas Then
Some options are:
1) If UCase(rA.Range(1, 2)) = UCase(MyInputMatriculaMaiusculas) Then
2) If StrComp(rA.Range(1, 2), MyInputMatriculaMaiusculas, vbTextCompare) = 0 Then
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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