简体   繁体   English

Excel VBA:在for循环中跳过复制的单元格

[英]Excel VBA: Skip the copied cell in for loop

I have a some data where I have some conditions.我有一些数据,我有一些条件。 If each cell in column B contains words like "and", "or", "and/or", then create a copy of that row and insert it into next row following the copied row.如果 B 列中的每个单元格都包含诸如“and”、“or”、“and/or”之类的词,则创建该行的副本并将其插入到复制行之后的下一行。

Currently my data looks like this:目前我的数据是这样的: 在此处输入图片说明

This is my code:这是我的代码:

Sub Macro2()
    Dim rng As Range, cell As Range, rowRange As Range
        
    Set rng = Range("B1", Range("B1").End(xlDown))
    
    Dim values As Variant
    Dim Result() As String
    connectorArray = Array("and/or", "or", "and")
    Dim findConnectorWord As String
        
    'Worksheets("Sheet1").Activate
    'Range("B1", Range("B1").End(xlDown)).Select
    
    For Each cell In rng
        findConnectorWord = FindString(cell.Value, connectorArray)
        If findConnectorWord <> vbNullString Then
            Result() = Split(cell, findConnectorWord)
            Set rowRange = Range("A" & cell.Row, Range("B" & cell.Row).End(xlToRight))
            rowRange.Copy           
            rowRange .Offset(1, 0).Insert Shift:=xlDown
    
            'Logic to skip the next cell

        End If
    Next cell    
End Sub

Function FindString(SearchString As String, arr As Variant) As String
    For Each searchWord In arr
        If InStr(SearchString, searchWord) > 0 Then
            FindString = searchWord
            Exit For
        End If
    Next
End Function

The problem that I am having is that once the row is copied and inserted into the next row, the next iteration reads the copied row("Homeowners or Dwelling Fire") and creates another copy.我遇到的问题是,一旦该行被复制并插入下一行,下一次迭代就会读取复制的行(“房主或住宅火灾”)并创建另一个副本。 What I would like to do is to skip the cell once the row is copied, inside the if condition and look at Cell B3(Assuming that Umbrella (C) gets pushed down when the new cell is copied over).我想要做的是在复制行后跳过单元格,在 if 条件内并查看单元格 B3(假设在复制新单元格时伞 (C) 被推下)。 What's the best possible way to do this?什么是最好的方法来做到这一点?

One of the possible options for implementing what @freeflow wrote about in his comment:实现@freeflow 在他的评论中所写内容的可能选项之一:

    ...
    Set cell = Range("B1").End(xlDown)  ' start from last cell
    Do Until False
        findConnectorWord = FindString(cell.Value, connectorArray)
        If findConnectorWord <> vbNullString Then
            ...
            Set rowRange = cell.EntireRow
            rowRange.Copy
            rowRange.Offset(1, 0).Insert Shift:=xlDown
        End If
        If cell.Row = 1 Then Exit Do ' First row? Enough
        Set cell = cell.Offset(-1, 0)   ' Shift up
    Loop
   ...

And one more note - when defining values ​​for connectorArray, add spaces to the terms: " and " instead of "and" .还有一点要注意 - 在为 connectorArray 定义值时,在术语中添加空格: " and "而不是"and" Otherwise, you can duplicate the line with some Brandon or Alexandra否则,您可以使用一些 Brandon 或 Alexandra 复制该行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM