对于i = 2到thisworkbook.worksheets.count,通过工作簿中的所有工作表

时间:2017-10-05 10:45:37

标签: excel vba for-loop worksheet

任何人都可以帮助我吗?我试图让我的代码循环遍历工作簿中的所有工作表。它完美地适用于一个工作表,但我正在努力使其循环遍历所有工作表

<p *myDirective="type">{{err}}</p> 

2 个答案:

答案 0 :(得分:0)

要循环使用所有工作表的代码,请使用以下代码。

Sub stringcheck()

Dim MainString As String
Dim SubString As String
Dim Lastrow As Long, Lcount As Long
Dim i As Integer, j As Integer
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
ws.Activate
SubString = "All Grps"

Lastrow = ActiveSheet.Range("A30000").End(xlUp).Row


For i = 3 To Lastrow
    MainString = Range("B" & i)
        If InStr(MainString, SubString) <> 0 Then
            Rows(Range("A" & i).Row + 1 & ":" & Range("A" & i).Row + 2).Insert
        End If
Next i
Next ws
End Sub

答案 1 :(得分:0)

你可以尝试一下......

Sub stringcheck()
    Dim ws As Worksheet
    Dim MainString As String
    Dim SubString As String
    Dim Lastrow As Long, Lcount As Long
    Dim i As Integer, j As Integer

    Application.ScreenUpdating = False

    SubString = "All Grps"

    For Each ws In ActiveWorkbook.Worksheets
        Lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
        For i = Lastrow To 3 Step -1
            MainString = ws.Range("B" & i)
            If InStr(MainString, SubString) <> 0 Then
                ws.Range("A" & i + 1).Resize(2).EntireRow.Insert
            End If
        Next i
    Next ws
    Application.ScreenUpdating = True
End Sub