+ Reply to Thread
Results 1 to 4 of 4

Thread: Delete a sheet from Workbook

  1. #1
    Registered User
    Join Date
    07-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Delete a sheet from Workbook

    Hello,

    I am trying to write a code which checks if a chart (named "Friction - Chart "plotted as a seperate sheet) is existing. If yes, the code deletes the chart sheet. However my code fails to that. If however I try to do the same with another sheet within the workbook called "Friction" (it contains just 2 columns of data), it executes it perfectly. So I am at a loss trying to figure out why the code works for one sheet but not the other. Below is the code:

    Code:
    Sub deletesheet()

    Dim sh As Worksheet

    ' Delete any existing charts.
    For Each sh In ThisWorkbook.Worksheets
    If sh.Name Like "Friction - Chart" Then
    Application.DisplayAlerts = False
    sh.Delete
    Application.DisplayAlerts = True
    End If
    Next sh

    End Sub

    I am also attaching the excel spreadsheet with the macro. Can somebody please help me figure out why such a thing is happening and suggests changes to the code?

    Thanks
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Delete a sheet from Workbook

    Hi parasbshah,

    It looks like your chart is a "Chart" not a "Worksheet"

    Looking through the object browser (I hate the thing but it helps) it shows you need to use
    For each Chart instead of Worksheet.

    In the immediate window do a
    ?charts(1).Name
    to see it is a chart and not a worksheet.

    hope that helps.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Delete a sheet from Workbook

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Delete a sheet from Workbook

    hi
    try this
    Sub Button1_Click()
    ActiveWorkbook.Charts.Delete
    End Sub
    Regards, John

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0