Excel 循环浏览所有工作表问题

Excel 循环浏览所有工作表问题,excel,vba,Excel,Vba,我有一个长宏,它的末尾有以下内容: On Error Resume Next For Each ws In ActiveWorkbook.Worksheets ws.Activate Do If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlD

我有一个长宏,它的末尾有以下内容:

        On Error Resume Next

    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Do
        If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
            Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
            iRow = iRow + 2
        Else
            iRow = iRow + 1
        End If
        Loop While Not Cells(iRow, iCol).Text = ""

        ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats

    Next ws
出错时继续下一步
对于ActiveWorkbook.Worksheets中的每个ws
ws.Activate
做
如果单元格(iRow+1,iCol)单元格(iRow,iCol),则
单元格(iRow+1,iCol).EntireRow.Insert Shift:=xlDown
iRow=iRow+2
其他的
iRow=iRow+1
如果结束
非单元格时循环(iRow,iCol)。Text=“”
ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
下一个ws

当B列发生更改(分隔数据组),然后删除空白行上的格式化时,应该添加一个空白行。 这似乎并没有正确地循环浏览所有工作表,因为在B列中进行更改后,只有第一张工作表被更改为包含一个空行。这也是非常缓慢的

我希望我能在这方面得到一些帮助,也许是更快的更好的解决方案


提前感谢您的帮助。

在工作表中循环时,您必须将设置为正值在引用范围时添加工作表引用。否则,任何范围引用都将引用
ActiveSheet
中的任何内容

On Error Resume Next

For Each ws In ThisWorkbook.Worksheets
    With ws
        Do
            If .Cells(iRow + 1, iCol) <> .Cells(iRow, iCol) Then
                .Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
                iRow = iRow + 2
            Else
                iRow = iRow + 1
            End If
        Loop While Not .Cells(iRow, iCol).Text = ""
        .UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
    End With
Next ws
出错时继续下一步
对于此工作簿中的每个ws。工作表
与ws
做
如果.Cells(iRow+1,iCol).Cells(iRow,iCol),则
.Cells(iRow+1,iCol).EntireRow.Insert Shift:=xlDown
iRow=iRow+2
其他的
iRow=iRow+1
如果结束
不循环时循环。单元格(iRow,iCol)。Text=“”
.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
以
下一个ws

在图纸中循环时,必须使为正值在引用范围时添加工作表引用。否则,任何范围引用都将引用
ActiveSheet
中的任何内容

On Error Resume Next

For Each ws In ThisWorkbook.Worksheets
    With ws
        Do
            If .Cells(iRow + 1, iCol) <> .Cells(iRow, iCol) Then
                .Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
                iRow = iRow + 2
            Else
                iRow = iRow + 1
            End If
        Loop While Not .Cells(iRow, iCol).Text = ""
        .UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
    End With
Next ws
出错时继续下一步
对于此工作簿中的每个ws。工作表
与ws
做
如果.Cells(iRow+1,iCol).Cells(iRow,iCol),则
.Cells(iRow+1,iCol).EntireRow.Insert Shift:=xlDown
iRow=iRow+2
其他的
iRow=iRow+1
如果结束
不循环时循环。单元格(iRow,iCol)。Text=“”
.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
以
下一个ws

正如jsheeran评论的那样,您必须在循环的每个新工作表上初始化iRow

一种方法是从iCol列中最后一个非空单元格行向后循环到第二个,这也简化了代码:

For Each ws In ActiveWorkbook.Worksheets
    With ws
        For iRow = .Cells(.Rows.Count, iCol).End(xlUp) To 2 Step - 1
            If .Cells(iRow - 1, iCol) <> .Cells(iRow, iCol) Then .Cells(iRow, iCol).EntireRow.Insert Shift:=xlDown
        Next
        On Error Resume Next
        .UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
        On Error GoTo 0
    End With
Next
ActiveWorkbook.工作表中每个ws的

与ws
对于iRow=.Cells(.Rows.Count,iCol)。结束(xlUp)到2步骤-1
如果.Cells(iRow-1,iCol).Cells(iRow,iCol)那么.Cells(iRow,iCol).EntireRow.Insert Shift:=xlDown
下一个
出错时继续下一步
.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
错误转到0
以
下一个

正如jsheeran评论的那样,您必须在循环的每个新工作表上初始化iRow

一种方法是从iCol列中最后一个非空单元格行向后循环到第二个,这也简化了代码:

For Each ws In ActiveWorkbook.Worksheets
    With ws
        For iRow = .Cells(.Rows.Count, iCol).End(xlUp) To 2 Step - 1
            If .Cells(iRow - 1, iCol) <> .Cells(iRow, iCol) Then .Cells(iRow, iCol).EntireRow.Insert Shift:=xlDown
        Next
        On Error Resume Next
        .UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
        On Error GoTo 0
    End With
Next
ActiveWorkbook.工作表中每个ws的

与ws
对于iRow=.Cells(.Rows.Count,iCol)。结束(xlUp)到2步骤-1
如果.Cells(iRow-1,iCol).Cells(iRow,iCol)那么.Cells(iRow,iCol).EntireRow.Insert Shift:=xlDown
下一个
出错时继续下一步
.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
错误转到0
以
下一个

在循环的每次迭代中,您可能希望将
iRow
iCol
设置为1,否则它会越来越大。如果您避免
激活
-ing工作表,您应该会看到性能改进@BruceWayne的答案正是如此。在循环的每次迭代中,您可能希望将
iRow
iCol
设置为1,否则它会越来越大。如果您避免
激活
-ing工作表,您应该会看到性能改进@BruceWayne的回答正是如此。我想摆脱
。激活
也应该解决OP关注的速度问题。感谢您的回复,非常感谢。然而,这段代码和我的代码一样,实际上并没有遍历所有的工作表。我已经设法解决了这个问题。我在ClearFormats行后面放置了以下内容:Set oRng=Range(“b1”)iRow=oRng.Row iCol=oRng。Column@Princey-小心,确保符合第一个
范围(“b1”)
与工作表一起使用。我想摆脱
.Activate
也应该解决OP关注的速度问题。感谢您的回复,非常感谢。然而,这段代码和我的代码一样,实际上并没有遍历所有的工作表。我已经设法解决了这个问题。我在ClearFormats行后面放置了以下内容:Set oRng=Range(“b1”)iRow=oRng.Row iCol=oRng。Column@Princey-小心,确保使用工作表限定第一个
范围(“b1”)
。感谢您的回复。我收到以下错误:此部分上的“应用程序定义或对象定义”错误“1004”:If.Cells(iRow-1,iCol)Cells(iRow,iCol)(第4行)感谢您的回复。我收到以下错误:此部分上的“应用程序定义或对象定义”错误“1004”:If.Cells(iRow-1,iCol)Cells(iRow,iCol)(第4行)