【问题标题】:Excel macro to run for all sheets except one对除一张以外的所有工作表运行的 Excel 宏
【发布时间】:2019-08-16 20:38:28
【问题描述】:

下面的 Excel 宏在几个预先确定的工作表上运行并设置它们的格式。我正在寻找对其进行修改以使其格式化工作簿中的所有工作表,但一张除外。

所有工作表,无论名称如何,都应格式化,查找工作表除外。

下面列出的代码被缩短为只显示一张纸,它对其他 10 张纸做完全相同的事情,但我不想把所有的都放在这里。谢谢。

Sub Formatting()


Application.ScreenUpdating = False


Sheets("Products").Select
Columns("F:W").Select
Selection.EntireColumn.Hidden = True
Range("X1").Select
Selection = "Product #"
Range("X1").Font.Bold = True


With Sheets("Products")
    .Range("X2:X" & .Cells(.Rows.Count, "A").End(xlUp).row).Formula = "=IF(ISNA(VLOOKUP(S2,Lookup!C:D,2,FALSE)),"""",VLOOKUP(S2,Lookup!C:D,2,FALSE))"
    .Range("Z2:Z" & .Cells(.Rows.Count, "A").End(xlUp).row).Formula = "=IF(ISNA(VLOOKUP(S2,Lookup!C:D,2,FALSE)),"""",VLOOKUP(S2,Lookup!C:D,2,FALSE))"
    .Range("AC2:AC" & .Cells(.Rows.Count, "A").End(xlUp).row).Formula = "=IF(Z2="""","""",E2)"
End With


Range("A1:X50").Select
ActiveWorkbook.Worksheets("Products").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Products").Sort.SortFields.Add _
    Key:=Range("X2:X50"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Products").Sort
    .SetRange Range("A1:X50")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Columns("X:X").ColumnWidth = 9

Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 使用循环会好得多。我假设你有这段代码 11 次,为什么不写一次,让代码循环 11 次呢?使用这样的循环,您可以添加一个If 语句来确定当前迭代中的工作表是否是您要跳过的工作表,并相应地这样做excelhelphq.com/…
  • 此外,最好避免使用SelectActivate 函数。阅读以下链接:) riptutorial.com/excel-vba/example/9292/…

标签: excel vba


【解决方案1】:

遍历工作表是关键:

Option Explicit
Sub Formatting()

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        If Not ws.Name = "LookUp" Then 'the sheet with the lookup name
            'your formatting code
        End If
    Next ws    

End Sub

【讨论】:

  • 谢谢@Damian 这让它变得简单多了。
猜你喜欢
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多