从行3584中删除VBA中的行不起作用

时间:2015-04-13 07:23:05

标签: vba row

我想检查excel文件中的所有行,如果单元格包含某个值:删除它。

我的代码一直有效,直到第3584行,Rows(ActiveCell.row).Delete无效...

我没有错误消息,当我想用​​“F8”手动调试时,该行转到下一行而不删除它。

3584次迭代时的变量值:

i = 3584;
j = 3614;
ligne = 3584;
lastRow = 78216;
Var = "PROPERTY NAME = ..."
你能帮帮我吗? 我使用Excel 2007。

我的代码:

Sub ParcoursLignes()

'Selection de la feuille
Sheets("Feuil3").Select

Dim i As Long
Dim j As Long
Dim ligne As Long
Dim lastRow As Long

Var = 0
nomMachine = 0
nomMachine_brut = 0
ligneSupprime = 0
ligne = 0
i = 0
j = 0
lastRow = 0

'Selection de la 1ère cellule
Range("A1").Select

'Recupère la ligne de la dernière valeur de la colonne A
lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).row

For i = 1 To 40000 Step 1

If j <= 40000 Then

j = j + 1

'Récupérer le numéro de la ligne
ligne = ActiveCell.row


'Récupère le contenu de la cellule
Var = ActiveCell.Value

'test si la cellule contient "ip_address"
If Var Like "*ip_address*" Then

nommachine = toto
nomMachine_brut = 1010

ElseIf (Var Like "*PROPERTY*") Or (Var Like "*OBJECT*") Then
'Suppression de la ligne
Rows(ActiveCell.row).Delete
ligneSupprime = 1
End If

'Si ligneSupprime différent de 1
If ligneSupprime <> 1 Then
'Retour dans la colonne A
Range("A" & (ActiveCell.row + 1)).Select

Else
'retourne à la ligne précédente
i = i - 1
End If

ip = 0
Var = 0
nomMachine = 0
nomMachine_brut = 0
ligneSupprime = 0

Else
GoTo sortie
End If

Next i

sortie:

End Sub

1 个答案:

答案 0 :(得分:0)

我建议你在循环中向后移动

 For i = 40000 To 1 Step -1