格式设置范围为特定货币格式所有工作表

Format set range to specific currency format all worksheets

不确定为什么这不起作用,但我的想法是打开所有工作表,取消保护,将单元格格式化为货币,再次保护,同时启用过滤器。这适用于保护和取消保护但尝试格式化我收到一条错误消息:

Unable to set the numberformat property of the range class

Sub UnProtect()

    Dim cell_to_format As Range

      ' Loop through all sheets in the workbook

      For i = 1 To Sheets.Count

         Sheets(i).UnProtect "T1T3NPM"

         With ActiveSheet

       Range("L8:L99999").Cells.NumberFormat = "£#,##0.00"

        Range("O8:O99999").NumberFormat = "£#,##0.00"

        Range("R8:R99999").NumberFormat = "£#,##0.00"

        Range("U8:U99999").NumberFormat = "£#,##0.00"

        Range("X8:X99999").NumberFormat = "£#,##0.00"

        Range("AA8:AA99999").NumberFormat = "£#,##0.00"

        Range("AD8:AD99999").NumberFormat = "£#,##0.00"

    .Protect Password:="T1T3NPM", AllowFiltering:=True
    .EnableSelection = xlUnlockedCells
End With

      Next i

End Sub

提前致谢

With ActiveSheet 应该是 With Sheets(i)

您没有保护 Sheets(i),但正在使用 Activesheet,它可能是也可能不是 Sheets(i)

另外你需要使用 Worksheets 而不是 Sheets 否则如果有图表你会得到一个错误。

所以把Sheets(i)改成Worksheets(i)

您还需要完全限定范围对象,否则它将与 Activesheet 一起使用,而不是与 Worksheets(i)

一起使用
With Worksheets(i)
    '~~> Notice the DOT before Range
    .Range("L8:L99999").Cells.NumberFormat = "£#,##0.00"
End With

你可以试试:

Sub UnProtect()

    Dim ws As Worksheet
    Dim i As Long

    For Each ws In ThisWorkbook.Worksheets

      With ws

          .UnProtect "T1T3NPM"

          For i = 12 To 30 Step 3

              .Range(.Cells(8, i), .Cells(99999, i)).NumberFormat = "£#,##0.00"

          Next i

          .Protect Password:="T1T3NPM", AllowFiltering:=True
          .EnableSelection = xlUnlockedCells

      End With

    Next ws

End Sub