2016-04-07 44 views
0

所以我仍然是VBA的新手,我在理解它的所有语法方面遇到了一些麻烦。我试图循环访问一列,看看它是否包含部分字符串。如果没有,我需要将该行删除。到目前为止,我有这个:在循环中包含一个字符串的VBA单元

Private Sub CommandButton1_Click() 

Dim count As Integer 

Do While Range("H" & count).Value > 0 
    Dim exists As Integer 
    exists = InStr(1, Range("H" & count).Value, ".AB", vbTextCompare) 

    If exists > 0 Then 
     Rows(count).Delete 
    Else 
     count = count + 1 
    End If 
Loop 

End Sub 

但使用此代码,我得到各种错误。首先是对象'_worksheet'的'方法范围'失败。有关于此的任何知识?

+2

你没有在一开始初始化'与任何count'所以它等于0'H0'是不是一个有效的单元格。 – findwindow

+2

'count'为0,没有第0行。 –

+0

好的,所以我加了count = 2。但现在当我点击按钮时,绝对没有任何反应。 –

回答

0

请尝试下面的代码

Private Sub CommandButton1_Click() 
    Dim exists As Integer 
    Dim count As Integer 
    lastrow = Range("H" & Rows.count).End(xlUp).Row 
    For i = 2 To lastrow 
     If InStr(UCase(Range("H" & i).Value), ".AB") > 0 Then 
      Rows(i).Delete 
      lastrow = lastrow - 1 
     End If 
    Next i 
End Sub 
相关问题