简体   繁体   English

VBA:在找到的单元格之前插入新行

[英]VBA: insert a new row before found cell

Below is the code that I'm running, I'm not too sure if I can get what I want. 下面是我正在运行的代码,我不太确定我是否能得到想要的东西。
But strangely, the code is taking too long to run. 但是奇怪的是,代码花费的时间太长了。
I was waiting for it for an hour. 我等了一个小时。
So I figured there must be some errors in the code. 所以我认为代码中肯定有一些错误。
Can anybody shed some light on this? 有人能对此有所启示吗?

Set x = Workbooks.Open("C:\Users\Desktop\testing.xlsx")

For Each ws In x.Worksheets
If ws.Name <> "Master" Then
    lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = ws.Range(Cells(1, 1), Cells(lrow, 1))
    For Each Acell In rng
        If (Acell.Value = "Sum") Then
            Acell.Offset(-1, 0).EntireRow.Insert
        End If
    Next Acell
End If
Next ws

I'm looking for that "Sum" word in Col A, for every sheets in workbook except "master" sheet. 我正在工作簿A中除“主”工作表以外的所有工作表中寻找“和”字。

When you insert a row and then continue with the loop you will end up on a "Sum"-row again (it has been pushed one row down due to the Insert). 当您插入一行然后继续循环时,您将再次遇到“求和”行(由于“插入”已将其向下推了一行)。 Therefore your code will insert row after row after row after row... forever 因此,您的代码将一排又一排地插入...永远

Try this code (inside the If)... not the best solution perhaps, but it works: 尝试以下代码(在If中)...也许不是最好的解决方案,但是它可以工作:

lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
R = 1
Do Until R > lastrow
    If (ws.Cells(R, 1).Value = "Sum") Then
        ws.Cells(R, 1).EntireRow.Insert
        R = R + 1             ' Jump one extra step (to skip the inserted row)
        lastrow = lastrow+ 1  ' The last row is increased due to the inserted row
    End If
    R = R + 1
Loop

Since there is only 1 sum in a column 由于一列中只有1个和

Set x = Workbooks.Open("C:\Users\Desktop\testing.xlsx")

For Each ws In x.Worksheets
If ws.Name <> "Master" Then
    lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = ws.Range(Cells(1, 1), Cells(lrow, 1))
    For Each Acell In rng
        If (Acell.Value = "Sum") Then
            Acell.Offset(-1, 0).EntireRow.Insert
        exit for
        End If
    Next Acell
End If
Next ws

try this code. 试试这个代码。

You are looping through the worksheets but not using each individual worksheet as the parent worksheet to the cells. 您正在遍历工作表,但没有使用每个单独的工作表作为单元格的父工作表。 Without a qualified parent worksheet, each Cells simply defaults to the ActiveSheet. 没有合格的父级工作表,每个单元仅默认为ActiveSheet。

For Each ws In x.Worksheets
    If ws.Name <> "Master" Then
        with ws
            lrow = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set rng = .Range(.Cells(1, 1), .Cells(lrow, 1))
            For Each Acell In rng
                If Acell.Value = "Sum" Then
                    Acell.Offset(-1, 0).EntireRow.Insert
                End If
            Next Acell
        end with
    End If
Next ws

Note that you are starting in row 1 and if A1 is Sum then you are trying to insert a row at row 0. There is no row zero. 请注意,您从第1行开始,如果A1为Sum,则尝试在第0行插入一行。没有第0行。

Instead of the For Each loop use a For i = lRow to 1 Step -1 and then use i to access each row: If ws.Cells(i, 1) = "Sum" Then ws.Rows(i).Insert . 代替For Each循环,使用For i = lRow to 1 Step -1 ,然后使用i访问每一行: If ws.Cells(i, 1) = "Sum" Then ws.Rows(i).Insert

When looping from the bottom, then inserting/deleting rows doesn't affect rows above (which are not processed yet) but just rows below (which are already processed). 从底部循环时,插入/删除行不会影响上方的行(尚未处理),只会影响下方的行(已处理)。

Dim iRow As Long, lRow As Long
Dim ws As Worksheet
Dim x As Workbook

Set x = Workbooks.Open("C:\Users\Desktop\testing.xlsx")

For Each ws In x.Worksheets
    If ws.Name <> "Master" Then
        lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        For iRow = lRow To 1 Step -1 'walk through rows from last row (lRow) to first row going upwards (Step -1)
            If ws.Cells(iRow, 1) = "Sum" Then
                ws.Rows(iRow).Insert xlDown 'insert new row and move other rows down
                Exit For  'if there is only one "Sum" you can exit for here (to shorten the run time) 
                          'if there are more that one "Sum" in a sheet then remove that line.
            End If
        Next iRow
    End If
Next ws

Have you tried setting Workbook Calculation to Manual, i found this helpful since I tend to work with workbooks with a load of sheets an every sheets has a load of formulas. 您是否尝试过将“工作簿计算”设置为“手动”,我发现这很有用,因为我倾向于使用工作量很大的工作簿,每张工作表都有公式。

File>Options>Formulas

then set Workbook Calculations to manual, but if you want to automate this. 然后将“工作簿计算”设置为“手动”,但是如果要使其自动化。

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+e
'
    calcu
    'your code here

    ActiveWorkbook.Application.Calculation  = xlCalculationAutomatic

End Sub

'= '=

Function calcu()

    Dim xlCalc As XlCalculation

    xlCalc = Application.Calculation
    ActiveWorkbook.Application.Calculation = xlCalculationManual
    On Error GoTo CalcBack
    Exit Function

CalcBack:
    ActiveWorkbook.Application.Calculation = xlCalc

End Function

I got this from somewhere in stack but I forgot which post, so if you're the one who did this, thanks. 我是从堆栈中的某个地方得到的,但是我忘记了哪个帖子,所以如果您是这样做的人,谢谢。 :) :)

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

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