我正在尝试在50个标签中运行以下宏。 Autofilter宏确实可以跨选项卡工作,但是我希望将每个数字除以总数字的宏不起作用。基本上,从第5行开始的所有内容都不会应用于所有标签,而只会应用于一个标签。有人可以帮忙吗?
Sub Macro2()
'
' Macro2 Macro
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1:AC91").AutoFilter Field:=7, Criteria1:=Array("11" _
, "21", "22", "23", "31-33", "42", "44-45", "48-49", "51", "52", "53", "54", "55", "56", "61" _
, "62", "71", "72", "81"), Operator:=xlFilterValues
Range("AD1").Select
ActiveCell.FormulaR1C1 = "In %"
Range("AD1").Select
Selection.Font.Bold = True
Range("AD2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]/R2C28"
Range("AD2").Select
Selection.Copy
Range("AD2:AD91").Select
Application.CutCopyMode = False
Selection.FillDown
Selection.Style = "Percent"
Selection.NumberFormat = "0.0%"
Selection.Font.Bold = True
Range("AD2").Select
Next ws
End Sub
答案 0 :(得分:2)
您需要在以Range
和ws.
开头的每一行前加上前缀,否则,您将定位ActiveWorksheet,而不是循环中的那一行。
Sub Macro2()
'
' Macro2 Macro
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1:AC91").AutoFilter Field:=7, Criteria1:=Array("11" _
, "21", "22", "23", "31-33", "42", "44-45", "48-49", "51", "52", "53", "54", "55", "56", "61" _
, "62", "71", "72", "81"), Operator:=xlFilterValues
ws.Range("AD1").Select
ActiveCell.FormulaR1C1 = "In %"
ws.Range("AD1").Select
Selection.Font.Bold = True
ws.Range("AD2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]/R2C28"
ws.Range("AD2").Select
Selection.Copy
ws.Range("AD2:AD91").Select
Application.CutCopyMode = False
Selection.FillDown
Selection.Style = "Percent"
Selection.NumberFormat = "0.0%"
Selection.Font.Bold = True
ws.Range("AD2").Select
Next ws
End Sub