我有 3 列要在其中搜索文本。如果文本出现在任何一列中,我想添加一行并进行一些额外的配置。
但是,如果它连续出现不止一次,我希望它停止并移动到下一行。
逻辑存在:一次检查一行一列,如果出现ABC,插入行 set counter=1 if 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
关于excel - 在 Excel VBA 中执行 if-then-next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391132/