[英]Delete the row if the cell value on sheet is the same then the cell value on another sheet
I need to delete the row if the value of column G on a sheet named OV is = the value on column G on a sheet named all_teams.如果名为 OV 的工作表上 G 列的值 = 名为 all_teams 的工作表上 G 列的值,我需要删除该行。 I am trying different approach, however when it works only deletes 1 row.
我正在尝试不同的方法,但是当它起作用时只会删除 1 行。
Sub test_delete()
Dim j As Variant
Dim ltrow As Long
ltrow = OV.Range("C" & Cells.Rows.Count).End(xlUp).Row
For j = 2 To ltrow
If all_teams.Range("G" & j).Value = OV.Range("G" & ltrow).Value Then
OV.Rows(j).Delete
End If
Next j
End Sub
What would be the best approach for it?最好的方法是什么?
Learnt this from one of my previous questions.从我之前的一个问题中了解到这一点。
When deleting rows in a for loop, its best to do it backwards, so the following在 for 循环中删除行时,最好向后执行,因此以下
For j = ltRow To 2 Step -1
I believe this could help you out!我相信这可以帮助你!
sub test_delete()
dim j as variant
dim ltrow as long
ltrow = worksheets("OV").Range("C" & Rows.Count).End(xlUp).Row
for j = ltRow to 2 Step -1
if sheets("all_teams").Range("G" & J).Value = Sheets("OV").Range("G" & ltrow).value then
Sheets("OV").Rows(J).entirerow.delete
end if
next
end sub
untested, but I think it may work!未经测试,但我认为它可能有效!
Try:尝试:
Option Explicit
Sub test_delete()
Dim j As Variant
Dim ltrow As Long
Dim wsOV As Worksheet, wsAll As Worksheet
With ThisWorkbook
Set wsOV = .Worksheets("OV")
Set wsAll = .Worksheets("all_teams")
End With
ltrow = wsOV.Range("C" & wsOV.Cells.Rows.Count).End(xlUp).Row
For j = ltrow To 2 Step -1
If wsAll.Range("G" & j).Value = wsOV.Range("G" & ltrow).Value Then
wsOV.Rows(j).EntireColumn.Delete
End If
Next j
End Sub
Try the next code, please.请尝试下一个代码。 Please, properly set the used worksheets:
请正确设置使用的工作表:
Sub test_delete()
Dim OV As Worksheet, all_teams As Worksheet, lastROV As Long, lastRAllT As Long
Dim i As Variant, j As Variant, rngDel As Range
'you have to set here the sheets...
lastROV = OV.Range("G" & cells.Rows.count).End(xlUp).Row
lastRAllT = all_teams.Range("G" & cells.Rows.count).End(xlUp).Row
For j = 2 To lastROV
For i = 2 To lastRAllT
If all_teams.Range("G" & i).Value = OV.Range("G" & j).Value Then
If rngDel Is Nothing Then
Set rngDel = OV.Rows(j)
Else
Set rngDel = Union(rngDel, OV.Rows(j))
End If
Exit For
End If
Next i
Next j
If Not rngDel Is Nothing Then rngDel.EntireRow.Delete xlUp
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.