VBA代码对所有未知名称的工作表和工作簿执行操作
我有很多报告,都有小计,我不需要。 他们都是唯一编号,然后是“总计”(粗体)。 我正在创build将应用于所有工作簿的代码,因为我将创build新的代码,以查找和删除其中包含“总计”(粗体)文本的任何行。 我知道如何将它应用到具有我知道的名称的特定工作簿/工作表,但不知道如何使它适用于任何具有任何名称的工作表。
Sub Find_Delete_Total() Dim Total_Row As Long Dim X As Long For Each Ws In ActiveWorkbook.Worksheets With Ws With Application.FindFormat.Font .FontStyle = "Bold" .Subscript = False .TintAndShade = 0 End With For X = 1 To 10000 Total_Row = Cells.Find(What:="Total", _ After:=Range("A1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Select.Row Selection.Delete Shift:=xlUp Next X End With Next Ws End Sub
您修改的代码缺less与With ActiveSheet
对应的End With
。 另外,将ActiveSheet
更改为Sht
,并限定 Sht
的适当对象。
我做了一些修改,应避免在找不到“总数”的情况下发生错误。
Sub Find_Delete_Total() Dim Total_Row As Long Dim X As Long Dim delRow as Range Dim Sht as Worksheet For Each Sht In ActiveWorkbook.WorkSheets With Application.FindFormat.Font .FontStyle = "Bold" .Subscript = False .TintAndShade = 0 End With With Sht For X = 1 To 10000 Set delRow = .Cells.Find(What:="Total", _ After:=.Range("A1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False, _ SearchFormat:=True) If Not delRow Is Nothing Then delRow.EntireRow.Delete Shift:=xlUp End If Next X End With Next Sht End Sub
For Each ws In ActiveWorkbook.Worksheets .... Next ws