Doing if- then- next in Excel VBA



我有3列,我正在搜索文本。如果文本出现在任何一列中,我想添加一行并做一些额外的配置。

但是,如果它在一行中出现多次,我希望它停止并移动到下一行。

逻辑存在:每次检查一行一列,如果ABC出现,插入行集counter=1如果counter=1跳到下一行

For x = 1 To 1000
  Count = 0
  For y = 1 To 19
    If Count = 1 Then Next x
    End If
    If Left(cell(x, y), 8) = "ABC" Then
      Rows(x+1).Insert
      Count = 1
    End If
  Next y
Next x
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
Dim x As Integer
Dim y As Integer
Dim Count As Integer
'Loop the rows
For x = 1 To 1000
    Count = 0
    'Check the columns
    For y = 1 To 19
        If Left(ws.Cells(x, y), 3) = "ABC" Then
            'Increment the counter if we found it
            Count = Count + 1
            'This will prevent looping all the columns once we have more than one occurrence.  This will help performance. 
            If Count > 1 Then
                Exit For
            End If
        End If
    Next y
    'After checking all the columns, add a row if we found the text only one time
    If Count = 1 Then
        ws.Rows(x+1).Insert
        'Do other stuff here
    End If
Next x

最新更新