Results 1 to 4 of 4

Thread: [RESOLVED] Clearing cells in a range?

  1. #1

    Thread Starter
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Resolved [RESOLVED] Clearing cells in a range?

    This should be a piece of cake.

    I have about 40 spreadsheets each with 12 worksheets (don't ask why - I inherited this )

    Anyway, end of financial year - I need to clear all the data in a specifed range on all worksheets on all workbooks.

    That's about 500 worksheets I need to delete - manually.

    I'd much rather do 40 workbooks than 500 worksheets so I was going to copy and paste this bit of code into each workbook - run it - watch the data get zapped and then go

    Code:
    Sub ClearWorksheet()
    Dim ws As Worksheet
    
       For Each ws In ActiveWorkbook.Worksheets
          Worksheets(ws).Range("A5", "AN905").ClearContents
       Next
    End Sub
    Should be simple - not asking for much and I'm sure it's just a syntax thing I'm missing.

    I intend on opening a workbook, pasting in the sub and then running it. Open next workbook etc etc.
    Rate my response if I helped

    Go Hard Or Go Home


  2. #2

    Thread Starter
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Re: Clearing cells in a range?

    Forgot to add :-

    The first clearcontents works, that is, worksheet 1, but fails on the second

    Run-time erro 1004
    Application-defined or object-defined error

    Yeah - ta. Thx for that error message.

    I've even tried this

    Code:
       Worksheets("Jul06").Range("A5:AN905").ClearContents
       Worksheets("Aug06").Range("A5:AN905").ClearContents
       Worksheets("Sept06").Range("A5:AN905").ClearContents
    But again - fails on the second clearcontents attempt.
    Rate my response if I helped

    Go Hard Or Go Home


  3. #3

    Thread Starter
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Re: Clearing cells in a range?

    Hmm. Seems the worksheets are protected (no password).

    BRB.
    Rate my response if I helped

    Go Hard Or Go Home


  4. #4

    Thread Starter
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Re: Clearing cells in a range?

    Sorted. I'll just paste and run this in each workbook. I'll be done by the time you read my ramblings.

    Code:
    Sub ClearWorksheet()
    Dim ws As Worksheet
    
       For Each ws In ActiveWorkbook.Worksheets
          ws.Unprotect
          ws.Range("A5", "AN905").ClearContents
       Next
    End Sub
    Thanks for your help Gaz!
    Rate my response if I helped

    Go Hard Or Go Home


Posting Permissions

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



Click Here to Expand Forum to Full Width