我有3列我正在搜索文本。如果文本出现在任何一列中,我想添加一行并进行一些额外的配置。
但是,如果它连续出现多次,我希望它停止并移动到下一行。
逻辑存在:一次检查第一行,如果出现ABC,如果counter = 1则插入行设置计数器= 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
答案 0 :(得分:1)
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