让我的脚本在所有工作表中运行(Excel)

此代码在一张纸上工作,现在我试图让它在多张纸上工作,避免前两张(“AA”和“字频率”)

原始代码在这里 (见@ Jeeped的答案)

链接到工作表在这里

正在尝试从我find的相关线程调整代码(参考1,2 ),但是我不知道如何(和是否)将Ws.NameWs.Range对象应用到我现有的代码中。

它看起来像代码激活Sheet1使用With Worksheets("Sheet1") ,我试图用以下方法replace:

  1. 创buildFor循环函数byGroupCounter()以确定有多less工作表,并在所有现有的工作表中运行。 每个工作表将增加variables“我”

  2. For循环inGroupCounter()调用函数byGroup(i)在选定的工作表(即工作表“ i ”)上运行原始代码

  3. byGroup()函数在工作表i上运行它的过程。

  4. 部分我相信我得到一个错误:replaceWith Worksheets("Sheet1")代码With Ws Ws = Worksheets(Sheet_Index)Sheet_Index等于 ,从byGroupCounter()

我相信我必须在.Range前添加Ws前缀,但是我一直在尝试的一切,我总是收到错误“无法在中断模式下执行代码”。

当前代码:

 Sub byGroupCounter() Dim i As Integer Application.ScreenUpdating = False For i = ActiveSheet.Index To Sheets.Count byGroup i Next i Application.ScreenUpdating = True End Sub Sub byGroup(ByVal Sheets_Index As Integer) Dim g As Long, s As Long, aSTRs As Variant, aGRPs As Variant Dim Ws As Worksheet Set Ws = Worksheets(Sheet_Index) appTGGL bTGGL:=False ' I believe the next line is where I am doing something wrong: With Ws aSTRs = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Value2 With .Range(.Cells(1, 5), .Cells(Rows.Count, 1).End(xlUp).Offset(0, Application.Match("zzz", .Rows(1)) - 1)) .Resize(.Rows.Count, .Columns.Count).Offset(1, 0).ClearContents aGRPs = Ws.Cells.Value2 End With For s = LBound(aSTRs, 1) To UBound(aSTRs, 1) For g = LBound(aGRPs, 2) To UBound(aGRPs, 2) If CBool(InStr(1, aSTRs(s, 1), aGRPs(1, g), vbTextCompare)) Then aGRPs(s + 1, g) = aSTRs(s, 1) Exit For End If Next g Next s .Cells(1, 5).Resize(UBound(aGRPs, 1), UBound(aGRPs, 2)) = aGRPs End With appTGGL End Sub Public Sub appTGGL(Optional bTGGL As Boolean = True) Debug.Print Timer Application.ScreenUpdating = bTGGL Application.EnableEvents = bTGGL Application.DisplayAlerts = bTGGL Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual) End Sub 

原始代码只有6个变化循环通过工作表

我有他们评论“<<<

 Sub byGroup() Dim g As Long, s As Long, aSTRs As Variant, aGRPs As Variant, sh As Worksheet '<<< appTGGL bTGGL:=False For Each sh In Sheets '<<< If sh.Name <> "AA" And sh.Name <> "Word Frequency" Then '<<<< With sh '<<< aSTRs = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Value2 With .Range(.Cells(1, 5), .Cells(Rows.Count, 1).End(xlUp).Offset(0, Application.Match("zzz", .Rows(1)) - 1)) .Resize(.Rows.Count, .Columns.Count).Offset(1, 0).ClearContents aGRPs = .Cells.Value2 End With For s = LBound(aSTRs, 1) To UBound(aSTRs, 1) For g = LBound(aGRPs, 2) To UBound(aGRPs, 2) If CBool(InStr(1, aSTRs(s, 1), aGRPs(1, g), vbTextCompare)) Then aGRPs(s + 1, g) = aSTRs(s, 1) Exit For End If Next g Next s .Cells(1, 5).Resize(UBound(aGRPs, 1), UBound(aGRPs, 2)) = aGRPs End With End If '<<<< Next sh '<<< appTGGL End Sub Public Sub appTGGL(Optional bTGGL As Boolean = True) Debug.Print Timer Application.ScreenUpdating = bTGGL Application.EnableEvents = bTGGL Application.DisplayAlerts = bTGGL Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual) End Sub