excel - 在 Excel VBA 中执行 if-then-next

标签 excel vba

我有 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/

相关文章:

asp.net - Epplus loadfromCollection int 列表不起作用

excel - 跨多个模块的文件名

vba - 如何编写一个VBA函数,其返回值被Excel识别为日期?

vba - VBA 可以预测第 n 行并复制参数吗?

vba - 如果需要,根据主题行中的文本创建 Outlook 规则以创建文件夹

excel - 关闭时设置密码

php - Yii2 bower 错误heroku

C# Excel 互操作错误 : The file is being used by another process

excel - 在 vba 模块中使用 target.row 时出错

Excel 日期格式 - 季度和年份