[英]Delete entire row by checking adjacent column values using vba
我在B列中有一個帶有字符串的表格,大約有500行,我的單元格空白/填充了A列和C列到M的值。
我寫了一個宏,它將獲取B列中每行的字符串值,如果該行的相鄰單元格(從C列到M列)為空,則它將刪除整行。 但即使相鄰單元格中的任何一個具有值,它也會跳過該行。
這是我的表格
A B C D E F G H I J K L M
1.2 SERVER_P RE1 GR5
7.3 PROXY NET
4.5 NET CON V1 GR
預計它應該刪除整個2行,因為庫存C到M是空的。 我上面給出了三行,但是我的工作表包含大約500行數據。
這就是我現在擁有的
Dim rcount As Long, ccount As Long
Dim Count As Long, Lastrow As Long
Dim Targetname As String, Deletedname As String
Dim objFSObject As Object
Set objFSObject = CreateObject("Scripting.FileSystemObject")
Count = 0
Sheets("Sheet2").Activate
With ActiveSheet
'count the rows till which strings are there
Lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
For rcount = 1 To Lastrow
Targetname = Cells(rcount, 2).Value
Count = 0 'reset the counter
Cells(rcount, 2).Select
For ccount = 1 To 11
Deletedname = ActiveCell.Offset(0, ccount).Value
If Len(Trim$(Deletedname)) > 0 Then
ccount = 11
Else
Count = Count + 1
If Count = 11 Then
Rows(rcount).EntireRow.Delete
End If
End If
Next ccount
Next rcount
但是我的宏刪除了許多符合條件的行,即C到M都是空的,但是仍然沒有刪除很少的行,並且它們的單元格也從列C到M都是空的。
有人可以幫我這個。
在上面的@Logan Reed評論的幫助下
Dim rcount As Long, ccount As Long
Dim Count As Long, Lastrow As Long
Dim Targetname As String, Deletedname As String
Dim objFSObject As Object
Set objFSObject = CreateObject("Scripting.FileSystemObject")
Count = 0
Sheets("Sheet2").Activate
With ActiveSheet
'count the rows till which strings are there
Lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
For rcount = Lastrow to 1 Step -1
Targetname = Cells(rcount, 2).Value
Count = 0 'reset the counter
Cells(rcount, 2).Select
For ccount = 1 To 11
Deletedname = ActiveCell.Offset(0, ccount).Value
If Len(Trim$(Deletedname)) > 0 Then
ccount = 11
Else
Count = Count + 1
If Count = 11 Then
Rows(rcount).EntireRow.Delete
End If
End If
Next ccount
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.