VBA Excel在选项卡之间应用简单的划分

时间:2018-07-18 13:48:47

标签: excel vba excel-vba

以下代码运行属性无效使用错误。我只是想在50个标签上应用简单的划分。它基本上将每个单元格值除以顶部的一个单元格值。

Sub Macro2()
'
' Macro2 Macro
  Dim ws As Worksheet`
   For Each ws In ThisWorkbook.Worksheets
         ws.Range ("AD1")
         ActiveCell.FormulaR1C1 = "In %"
         ws.Range ("AD1")
         Selection.Font.Bold = True
         ws.Range ("AD2")
         Application.CutCopyMode = False
         Application.CutCopyMode = False
         ActiveCell.FormulaR1C1 = "=RC[-2]/R2C28"
         ws.Range ("AD2")
         Selection.Copy
         ws.Range ("AD2:AD91")
         Application.CutCopyMode = False
         Selection.FillDown
         Selection.Style = "Percent"
         Selection.NumberFormat = "0.0%"
         Selection.Font.Bold = True
         ws.Range ("AD2")
    Next ws
    End Sub

1 个答案:

答案 0 :(得分:4)

丢掉那些效率低下和不必要的选择,您的代码可以缩短为

Sub Macro2()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
     ws.Range("AD1").Value = "In %"
     ws.Range("AD1").Font.Bold = True
     With ws.Range("AD2:AD91")
        .FormulaR1C1 = "=RC[-2]/R2C28"
        .Style = "Percent"
        .NumberFormat = "0.0%"
        .Font.Bold = True
     End With
Next ws

End Sub