Consulting

Results 1 to 4 of 4

Thread: Clearing contents of all worksheets in a excel workbook

  1. #1

    Clearing contents of all worksheets in a excel workbook

    Hi all,

    Is there a simple VBA code to clear contents for all worksheets in a excel workbook?

    I have nearly 25-30 worksheets in a tool which need to be cleared before running the tool

    Thanks in advance for the help

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Always test code on backup copies of data.
    Sub ClearContents()
      Dim ws As Worksheet
      For Each ws In Worksheets
        ws.UsedRange.ClearContents
      Next ws
    End Sub

  3. #3
    Quote Originally Posted by Kenneth Hobs View Post
    Always test code on backup copies of data.
    Sub ClearContents()
      Dim ws As Worksheet
      For Each ws In Worksheets
        ws.UsedRange.ClearContents
      Next ws
    End Sub
    thanks Kenneth. That works like a treat.

    Also, I have 28 sheets in my tool out of which I have to clear contents in 25 sheets. The other 3 sheets should remain untouched. Is there a smart way to do this rather than writing code for each sheet?

  4. #4
    Sub ClearContents()
      Dim ws As Worksheet
      For Each ws In Worksheets
        If Not (ws.Name = "FirstSheetToSkip" Or ws.Name = "SecondSheetToSkip" Or ws.Name = "ThirdSheetToSkip") Then
          ws.UsedRange.ClearContents
        End If
      Next ws
    End Sub

Posting Permissions

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