【问题标题】:How can I insert rows based on cell contents looped through all rows如何根据遍历所有行的单元格内容插入行
【发布时间】:2019-10-30 13:25:06
【问题描述】:

我正在尝试编写一个宏来整理和询问从某些分析仪器导出的原始数据。我希望它浏览所有行的一列(样本名称)并查找特定样本类型的指标,例如重复。找到这些指标我想插入一行,并在新插入的行中根据上面的两行做一些简单的计算。现在我会很高兴让行插入工作。

我可以让它找到关键字并插入 1 行,但它找到第一个并停止。我的数据中有多个这些关键字的实例,我想在每个实例下方插入一行

'original code - finds first keyword, inserts row and stops

Sub dup_finder()
    Dim colHeader As Range

    Set colHeader = Range("B1:B500")


Dim currCell As Range
Set currCell = Cells.Find("*_dup")

If Not currCell Is Nothing Then currCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown


End Sub

'my attempt to include loop - inserts 500 rows below keyword! stops 
after first instance

Sub dup_finder()
Dim colHeader As Range
Dim row As Long
Dim currCell As Range

Set colHeader = Range("B1:B500")

Set currCell = Cells.Find("_dup")
        For row = 1 To 500
If Not currCell Is Nothing Then currCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown

 Next row

End Sub

【问题讨论】:

  • 您的意思是要在 B 列中找到“_dup”的每个实例并在每个实例下插入一行?
  • 在循环和添加(或删除)行时,通常最好从底部开始向上工作,原因是当您添加一行时,原来的第 2 行现在是第 3 行,而是第 500 行现在是第 501 行(在您的循环范围之外)。因此,请尝试从 500 步变为 1 步 -1,看看是否能获得更好的结果。
  • 我想你要找的是FindNext。网上有很多这样的例子
  • 感谢您的帮助 - 不知道如何回复特定回复.. 但我会查找“FIndNext”和“您的意思是要在列中查找“_dup”的每个实例B 并在每个下插入一行? – SJR 7 分钟前” - 是的,正是这个!然后我想在该新行中包含一个简单的计算,但首先要做的是。

标签: excel vba loops if-statement


【解决方案1】:

我建议始终使用工作簿和工作表完全限定您的范围。

您应该能够根据自己的需要进行调整。您只需输入要签入的范围和要检查的值。

它向后工作,向上通过范围,在找到的每个下方插入一行。

    Sub InsertRows()
        ''Declare your variables
        Dim RngToCheck As Range, ValToFind As String
        ''Set the range in which to look for your desired string.
        Set RngToCheck = ThisWorkbook.Sheets("Sheet1").Range("B1:B500")
        ''Set what string to look for.
        ValToFind = "_dup"

        ''Declare a variable to use as a counter
        Dim i As Long
        ''Count backwards through each of the rows in the range.
        ''(If you went forwards through the range, the rows you
        ''are inserting would become part of that range and push
        ''the bottom rows (which you intended to check) out of the range).
        For i = RngToCheck.Rows.Count To 1 Step -1
            ''Check if the last characters (the number of characters to
            ''check is defined by the length of the string we are looking
            ''for) of the current cell match the string we are looking for.
            If Right(RngToCheck(i).Value, Len(ValToFind)) = ValToFind Then
                ''Insert the row (we need to offset by 1 row
                ''because rows are inserted ABOVE, and we
                ''want it BELOW the current cell).
                RngToCheck(i).Offset(1, 0).EntireRow.Insert
                ''Now you can add your formulas to the new row...
                ''column A
                RngToCheck(i).Offset(1, -1).Formula = "=1+1"
                ''column B
                RngToCheck(i).Offset(1, 0).Formula = "=2+2"
                ''column C
                RngToCheck(i).Offset(1, 1).Formula = "=A" & RngToCheck(i).Offset(1, 1).Row & "+B" & RngToCheck(i).Offset(1, 1).Row
                ''column D
                RngToCheck(i).Offset(1, 2).Formula = "Hello"
                ''And so on...
            End If
        Next i
    End Sub

【讨论】:

  • 这也有效!不擅长代码我不能评论如果你的解决方案比 SJR 的解决方案更好,但他们给了我关于做事的不同方法的想法 - 所以我真的很感激
  • 没问题。我已将 cmets 添加到代码中以进行澄清。
  • 感谢 cmets .. 努力让我明白如何/为什么有这么多不同的做事方式 - 尽管我想这就是赋予这种灵活性的原因.. 尝试和现在将公式合并到新行中,还是运行另一个循环以查找所选字符串(“_dup”)下方的行?我会尝试一些事情,可能会带着更多问题回来。
  • 您也可以这样做,但我现在会在您引用新行时添加公式。我会更新我的答案。
  • 我知道 cmets 不是用来谢谢你的……但谢谢你!真的很感激
【解决方案2】:

假设您确实想在 B 列中包含“_dup”的单元格的每个实例下插入一行,这应该可以工作。

您的代码的问题在于它没有循环,因此只找到了一个实例。

建议不要在插入行时指定固定范围,并且范围会扩大;但是,您可以这样做并将搜索方向设置为“上一个”。

Sub dup_finder()

Dim colHeader As Range, s As String

Set colHeader = Range("B1:B500") ' not actually used

Dim currCell As Range

'are we searching just B or the whole sheet?
Set currCell = Columns(2).Find(What:="_dup", Lookat:=xlPart, MatchCase:=False, SearchFormat:=False) 'change parameters to suit

If Not currCell Is Nothing Then
    s = currCell.Address             'store address of first found cell
    Do
        currCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown
        Set currCell = Columns(2).FindNext(currCell)  'find next case
    Loop Until currCell.Address = s  'keep looping until we are back to the original case
End If

End Sub

【讨论】:

  • 这非常有效,非常感谢包含的 cmets,因此我可以了解正在发生的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多