I am extremely new to VBA. I put together the below code to right align numbers and center align text in all sheets of a workbook. I defined the PerfArea.This code works in 3 different workbooks like a charm. In one of them I get the error message "Run-time error '1004' Method 'Range' of object'_Worksheet' failed. I don't think anything is different about this workbook. Any ideas? It looks like the problem line is the first ws.Range line. Thanks in advance.
Sub AlignCells()
Dim ws As Worksheet
Dim PerfArea As Range
For Each ws In ThisWorkbook.Worksheets
ws.Range("PerfArea").SpecialCells(xlCellTypeFormulas, 1).HorizontalAlignment = xlRight
On Error Resume Next
ws.Range("PerfArea").SpecialCells(xlCellTypeFormulas, 2).HorizontalAlignment = xlCenter
On Error Resume Next
ws.Range("PerfArea").SpecialCells(xlCellTypeConstants, 1).HorizontalAlignment = xlRight
On Error Resume Next
ws.Range("PerfArea").SpecialCells(xlCellTypeFormulas, 2).HorizontalAlignment = xlCenter
On Error Resume Next
Next ws
End Sub
Bookmarks