+ Reply to Thread
Results 1 to 2 of 2

Simple If statement throws an error

Hybrid View

  1. #1
    Registered User
    Join Date
    08-30-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    30

    Simple If statement throws an error

    My IF statement returns an error. I am guessing I have an End IF, or Else in the wrong place...


    Sub OpenNewSheet()
    
        Dim ans
        Dim ws As Worksheet
        
        If Left(ws.Name, 5) = "Sheet" And Left(ws.Name, 6) <> "Sheet1" And Left(ws.Name, 6) <> _
        "Sheet2" And Left(ws.Name, 6) <> "Sheet3" And Left(ws.Name, 6) <> "Sheet4" Then
        ws.Delete
        Else
           ans = MsgBox("Do you want to view this sheet?", vbYesNo)
                    If ans = vbYes Then
                        End
                    Else
                        ActiveSheet.Delete
                    End If
        End If
    
    End Sub
    Thanks

    Ronan

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Simple If statement throws an error

    You have no Sheet Object assigned to ws variable.

    Might be worth outlining what you're trying to achieve as I suspect you can condense the code etc...

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