Results 1 to 3 of 3

Macro works in one workbook and not another - Run-Time Error 1004

Threaded View

  1. #1
    Registered User
    Join Date
    06-10-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Macro works in one workbook and not another - Run-Time Error 1004

    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
    Last edited by vrobinson; 06-10-2010 at 07:31 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1