循环遍历除已命名的所有工作表
Loop Through All Worksheets Except Those Named
下面的代码用于设置我工作簿中所有工作表的列宽、对齐方式和格式,除了下面列出的,即:
- "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project List", "Rear Team Project List", "Acronyms".
然而代码只出现在 ActiveSheet
上的 运行,即 "Contents_Page":
Public Sub SheetCleanup()
Application.ScreenUpdating = False
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project
List", "Rear Team Project List", "Acronyms"
Case Else
ActiveSheet.Columns("g:g").NumberFormat = "dd-mm"
ActiveSheet.Columns("i:i").NumberFormat = "0"
ActiveSheet.Columns("B:K").HorizontalAlignment = xlLeft
ActiveSheet.Columns("G:G").HorizontalAlignment = xlCenter
ActiveSheet.Columns("A:A").HorizontalAlignment = xlCenter
ActiveSheet.Columns("H:H").HorizontalAlignment = xlCenter
ActiveSheet.Columns("I:I").HorizontalAlignment = xlCenter
ActiveSheet.Columns("J:J").HorizontalAlignment = xlCenter
ActiveSheet.Columns("A").ColumnWidth = 27
ActiveSheet.Columns("B").ColumnWidth = 50
ActiveSheet.Columns("C").ColumnWidth = 50
ActiveSheet.Columns("D").ColumnWidth = 21
ActiveSheet.Columns("E").ColumnWidth = 27
ActiveSheet.Columns("F").ColumnWidth = 21
ActiveSheet.Columns("G").ColumnWidth = 20
ActiveSheet.Columns("H").ColumnWidth = 18
ActiveSheet.Columns("I").ColumnWidth = 25
ActiveSheet.Columns("J").ColumnWidth = 24
ActiveSheet.Rows("3").HorizontalAlignment = xlCenter
End Select
Next sh
End Sub
ActiveSheet
在循环中没有改变,因此它通过它执行动作。在下面的代码中,它对已更改的 sh
执行操作。
一般来说,考虑把ActiveWorkbook
改成ThisWorkbook
看看How to avoid using Select in Excel VBA
Public Sub SheetCleanup()
Application.ScreenUpdating = False
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project List", "Rear Team Project List", "Acronyms"
Case Else
With sh
.Columns("g:g").NumberFormat = "dd-mm"
.Columns("i:i").NumberFormat = "0"
.Columns("B:K").HorizontalAlignment = xlLeft
.Columns("G:G").HorizontalAlignment = xlCenter
.Columns("A:A").HorizontalAlignment = xlCenter
.Columns("H:H").HorizontalAlignment = xlCenter
.Columns("I:I").HorizontalAlignment = xlCenter
.Columns("J:J").HorizontalAlignment = xlCenter
.Columns("A").ColumnWidth = 27
.Columns("B").ColumnWidth = 50
.Columns("C").ColumnWidth = 50
.Columns("D").ColumnWidth = 21
.Columns("E").ColumnWidth = 27
.Columns("F").ColumnWidth = 21
.Columns("G").ColumnWidth = 20
.Columns("H").ColumnWidth = 18
.Columns("I").ColumnWidth = 25
.Columns("J").ColumnWidth = 24
.Rows("3").HorizontalAlignment = xlCenter
End With
End Select
Next sh
End Sub
下面的代码用于设置我工作簿中所有工作表的列宽、对齐方式和格式,除了下面列出的,即:
- "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project List", "Rear Team Project List", "Acronyms".
然而代码只出现在 ActiveSheet
上的 运行,即 "Contents_Page":
Public Sub SheetCleanup()
Application.ScreenUpdating = False
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project
List", "Rear Team Project List", "Acronyms"
Case Else
ActiveSheet.Columns("g:g").NumberFormat = "dd-mm"
ActiveSheet.Columns("i:i").NumberFormat = "0"
ActiveSheet.Columns("B:K").HorizontalAlignment = xlLeft
ActiveSheet.Columns("G:G").HorizontalAlignment = xlCenter
ActiveSheet.Columns("A:A").HorizontalAlignment = xlCenter
ActiveSheet.Columns("H:H").HorizontalAlignment = xlCenter
ActiveSheet.Columns("I:I").HorizontalAlignment = xlCenter
ActiveSheet.Columns("J:J").HorizontalAlignment = xlCenter
ActiveSheet.Columns("A").ColumnWidth = 27
ActiveSheet.Columns("B").ColumnWidth = 50
ActiveSheet.Columns("C").ColumnWidth = 50
ActiveSheet.Columns("D").ColumnWidth = 21
ActiveSheet.Columns("E").ColumnWidth = 27
ActiveSheet.Columns("F").ColumnWidth = 21
ActiveSheet.Columns("G").ColumnWidth = 20
ActiveSheet.Columns("H").ColumnWidth = 18
ActiveSheet.Columns("I").ColumnWidth = 25
ActiveSheet.Columns("J").ColumnWidth = 24
ActiveSheet.Rows("3").HorizontalAlignment = xlCenter
End Select
Next sh
End Sub
ActiveSheet
在循环中没有改变,因此它通过它执行动作。在下面的代码中,它对已更改的 sh
执行操作。
一般来说,考虑把ActiveWorkbook
改成ThisWorkbook
看看How to avoid using Select in Excel VBA
Public Sub SheetCleanup()
Application.ScreenUpdating = False
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project List", "Rear Team Project List", "Acronyms"
Case Else
With sh
.Columns("g:g").NumberFormat = "dd-mm"
.Columns("i:i").NumberFormat = "0"
.Columns("B:K").HorizontalAlignment = xlLeft
.Columns("G:G").HorizontalAlignment = xlCenter
.Columns("A:A").HorizontalAlignment = xlCenter
.Columns("H:H").HorizontalAlignment = xlCenter
.Columns("I:I").HorizontalAlignment = xlCenter
.Columns("J:J").HorizontalAlignment = xlCenter
.Columns("A").ColumnWidth = 27
.Columns("B").ColumnWidth = 50
.Columns("C").ColumnWidth = 50
.Columns("D").ColumnWidth = 21
.Columns("E").ColumnWidth = 27
.Columns("F").ColumnWidth = 21
.Columns("G").ColumnWidth = 20
.Columns("H").ColumnWidth = 18
.Columns("I").ColumnWidth = 25
.Columns("J").ColumnWidth = 24
.Rows("3").HorizontalAlignment = xlCenter
End With
End Select
Next sh
End Sub