+ Reply to Thread
Results 1 to 5 of 5

Deleting worksheet only replaces worksheet name with a zero "0" number.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    365
    Posts
    163

    Deleting worksheet only replaces worksheet name with a zero "0" number.

    Is somebody able to tell me what's wrong with this code here?

    I'm simply trying to delete a worksheet in my activeworkbook based off of a variable name located in a cell below.

    I feel confident that this should be working based on the way that I have written it, but it's not doing what it's supposed to.

    Instead of completely deleting the worksheet, it's only deleting the "name" of the worksheet, and replacing it with zero "0".

    Sub DelVarWs()
    
    Dim sName As Integer, ws As Worksheet
    
    On Error Resume Next
    sName = ActiveCell.Offset(1, 0).value
    Set ws = ThisWorkbook.Worksheets(sName)
        
    Application.DisplayAlerts = False
    ws.Select
    ws.Delete
    Sheet1.Select
    Application.DisplayAlerts = True
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Deleting worksheet only replaces worksheet name with a zero "0" number.

    The code has in fact errors but hidden by the resume next.

    Option Explicit
    
    Sub DelVarWs()
    
    Dim sName As String, ws As Worksheet
    
    'On Error Resume Next
    sName = ActiveCell.Offset(1, 0).Value
    Set ws = ThisWorkbook.Worksheets(sName)
        
    Application.DisplayAlerts = False
    
    ws.Delete
    
    Application.DisplayAlerts = True
    
    End Sub
    sName must match the name on a workbook;otherise you will get out of range error

  3. #3
    Forum Contributor
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    365
    Posts
    163

    Re: Deleting worksheet only replaces worksheet name with a zero "0" number.

    Quote Originally Posted by AB33 View Post
    The code has in fact errors but hidden by the resume next.

    Option Explicit
    
    Sub DelVarWs()
    
    Dim sName As String, ws As Worksheet
    
    'On Error Resume Next
    sName = ActiveCell.Offset(1, 0).Value
    Set ws = ThisWorkbook.Worksheets(sName)
        
    Application.DisplayAlerts = False
    
    ws.Delete
    
    Application.DisplayAlerts = True
    
    End Sub
    sName must match the name on a workbook;otherise you will get out of range error
    I figured it out finally.

    Basically my issue had to do with me not identifying a cell address selection at the beginning of my code, and then I had to modify it to do a For Each Worksheet function.

    It's working great now. Thank you AB33 for giving me the rookie tip of commenting out the On Error so that I could better dig down into what was really going wrong.

    Sub DelVarWs()
    
        Dim rng As Range, sName As String, ws As Worksheet
            
        Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell 'These next two lines were the secret sauce
        rng.Select 'Sometimes, all it takes is a good nights sleep to come back and figure it all out
        
        sName = ActiveCell.Offset(1, 0).value
        Set ws = ThisWorkbook.Worksheets(sName)
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = sName Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
        Next ws
    
    End Sub

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Deleting worksheet only replaces worksheet name with a zero "0" number.

    You could also add exit for line in the sub once the code found the sheet so it will not loop unnecessarily.


    Sub DelVarWs()
    
        Dim rng As Range, sName As String, ws As Worksheet
            
        Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell 'These next two lines were the secret sauce
        rng.Select 'Sometimes, all it takes is a good nights sleep to come back and figure it all out
        
        sName = ActiveCell.Offset(1, 0).value
        Set ws = ThisWorkbook.Worksheets(sName)
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = sName Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
                exit for
            End If
        Next ws
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    365
    Posts
    163

    Re: Deleting worksheet only replaces worksheet name with a zero "0" number.

    Quote Originally Posted by AB33 View Post
    You could also add exit for line in the sub once the code found the sheet so it will not loop unnecessarily.


    Sub DelVarWs()
    
        Dim rng As Range, sName As String, ws As Worksheet
            
        Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell 'These next two lines were the secret sauce
        rng.Select 'Sometimes, all it takes is a good nights sleep to come back and figure it all out
        
        sName = ActiveCell.Offset(1, 0).value
        Set ws = ThisWorkbook.Worksheets(sName)
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = sName Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
                exit for
            End If
        Next ws
    
    End Sub
    Ooh... great idea!

    Thanks again AB33!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2018, 06:11 PM
  2. [SOLVED] if in worksheet "test1" is in cell G2 to G1000 an entry"share sale" copy the row new sheet
    By torti111 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-22-2017, 12:28 PM
  3. Replies: 6
    Last Post: 06-03-2016, 09:31 PM
  4. [SOLVED] Code Clean-Up: Delete "False" worksheet created when "Cancel" is chosen in Input Box
    By Kenny Blackwell in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 08-31-2015, 10:00 AM
  5. [SOLVED] Equation which searches a cell for PM or P and replaces number after with an "x"
    By herbie226 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-03-2013, 06:20 PM
  6. Moving entire row from the "Schedule" worksheet to "Completed" worksheet
    By Redveck in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2012, 02:30 PM
  7. Copy cells from worksheet "1" and paste into worksheet "2"?
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2011, 10:15 AM

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.6.0 RC 1