如果B列(“跟踪”工作表)中的值与B列(“活动”工作表)中的值相同,我想删除整行。 但是,即使我引用的两个值都是字符串类型,也总是会发生运行时错误13(类型不匹配)
代码如下:
Sub delete_row()
Dim active As Worksheet: Set activeSH = ThisWorkbook.Sheets("Active")
Dim Tracksheet As Worksheet: Set KPI = ThisWorkbook.Sheets("Track")
Dim i As Integer
Dim name As String
With Tracksheet
For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 4 Step -1
name = .Range("B" & i).Value
'Here I loop through each value in col B of Track sheet
'and reference it to values in col B of sheet "active"
If name = active.Range("B:B").Value Then 'this line where run time error 13 (type mismatch occurs)
.Rows(i).EntireRow.Delete
Else
End If
i = i - 1
Next i
End With
End Sub
非常感谢您的帮助!
答案 0 :(得分:1)
您可能想尝试这样的事情:
Sub delete_row()
Dim active As Worksheet
Dim Tracksheet As Worksheet
Dim i As Integer
Dim name As String
Dim cl As Range
Set active = ThisWorkbook.Sheets("Active")
Set Tracksheet = ThisWorkbook.Sheets("Track")
With Tracksheet
For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 4 Step -1
name = .Range("B" & i).Value
For Each cl In active.Range("B1:B100")
If name = cl.Value Then
.Rows(i).EntireRow.Delete
End If
Next cl
i = i - 1
Next i
End With
End Sub
您可以根据需要更改范围B1:B100。