每次我尝试对指定列中的每个单元格执行一次操作时,都会收到错误消息。我越来越了解vba,但似乎不了解循环方法。
这是我的代码:
Sheets("DB").Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter
'Application.ScreenUpdating = True
' ************** Si le systeme a planté *******************
''''''' ' filtre le IN USE
Sheets("Search Criteria").Range("O2").Value = "In Use"
'filter Locked by who based on the USER ID in DB
Dim Usedd As Range
Dim InUse As Range
Set Usedd = Sheets("Search Criteria").Range("O2")
Set InUse = Sheets("DB").Range("A1").CurrentRegion
With InUse
If Usedd.Value <> "" Then
Sheets("DB").Select
.AutoFilter field:=11, Criteria1:=Usedd.Value, Operator:=xlOr
End If
End With
'if IN USE then verify if there is an empty line that is reservecd but with no user id
Dim cell As Range
Dim LastRow As Long
For Each cell In Sheets("DB").Range("BP2:BP" & LastRow)
If cell.Value = "" And cell.Offset(0, -58).Value = "In Use" Then
cell.EntireRow.Delete
End If
Next cell
' ************** Fin de si le systeme a planté
Selection.AutoFilter
Selection.AutoFilter
Sheets("DB").Range("K1048576").Select
Selection.End(xlUp).Offset(1, 0).Select
Selection.Value = "In Use"
Selection.Offset(0, -1).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + 1
我得到的错误是对象未定义。 我似乎看不到代码中未定义的内容。 基本上我想做的是查看BP列。如果找到了客户端用户ID,请查看K列,如果K列在该行中正在使用中,则删除该行。
有人可以帮忙吗? 谢谢
答案 0 :(得分:2)
由于要在cell
范围对象上循环,请替换:
ActiveCell.EntireRow.Delete
使用
cell.EntireRow.Delete
别忘了声明所有变量,例如:
Dim cell As Range, Dim LastRow As Long
答案 1 :(得分:2)
删除行时,我会向后循环,还要适当地定义变量,Marcucciboy2提到的错误原因是您的LastRow变量从未初始化:
Dim counter As Long, LastRow As Long
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("DB")
'declare and set your variables above
LastRow = ws.Cells(ws.Rows.Count, "BP").End(xlUp).Row
'get the Last row with data in column BP to use in the following For Loop
For counter = LastRow To 2 Step -1 'loop from last row to the second row
If ws.Cells(counter, "BP").Value = "" And ws.Cells(counter, "K").Value = "In Use" Then
ws.Rows(counter).Delete
'GoTo Pas_plante
End If
Next counter