Consulting

Results 1 to 16 of 16

Thread: if sheet 9 exists, clear it otherwise create one

  1. #1
    VBAX Contributor
    Joined
    Apr 2012
    Posts
    107
    Location

    if sheet 9 exists, clear it otherwise create one

    Columns("A:B").Select
     Selection.Copy
    if sheet9 exists  then  sheet9.clear
    else Sheets.Add After:=Worksheets(ActiveSheet.Name)
      ActiveSheet.Name = "sheet9"
      end if
       ActiveSheet.Paste
    plse correct the above code

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Columns("A:B").Copy 
    On Error Resume Next
    Set sh = Worksheets("Sheet9")
    On Error GFoto 0
    If Not sh Is Nothing Then
        sh.UsedRange.ClearContents
    Else 
        Set sh = Sheets.Add(After:=ActiveSheet.Name) 
        sh.Name = "Sheet9" 
    End If 
    ActiveSheet.Paste
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Apr 2012
    Posts
    107
    Location
    Quote Originally Posted by xld View Post
    Columns("A:B").Copy 
    On Error Resume Next
    Set sh = Worksheets("Sheet9")
    On Error GFoto 0
    If Not sh Is Nothing Then
        sh.UsedRange.ClearContents
    Else 
        Set sh = Sheets.Add(After:=ActiveSheet.Name) 
        sh.Name = "Sheet9" 
    End If 
    ActiveSheet.Paste
    If Not sh Is Nothing Then
    the above code is not working
    Attached Files Attached Files

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Change the typo in the line
    On Error GFoto 0
    and run the code again
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Contributor
    Joined
    Apr 2012
    Posts
    107
    Location

    Syntax error
    Attached Images Attached Images

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Yes. Have a good look at the syntax. There is a simple typo error there.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    VBAX Contributor
    Joined
    Apr 2012
    Posts
    107
    Location
    sloved

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    For your reference, kindly go to the Thread Tools drop down and mark the thread as solved from there.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
       On Error Resume Next 
       Worksheets("Sheet9").UsedRange.ClearContents 
       if err.number <>0 then sheets.add.name="Sheet9"
    End Sub

  10. #10

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Interesting.... The OP is obviously frustrated with our help and posted elsewhere. When you look at the information posted here it's hard to see how we can do more given that "the above code isn't working" could mean anything.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    excelforum notified
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Interestingly. Chrome is blocking access to ExcelForum for me. ESET has long told me that ExcelForum is suspect when I follow a Google search that links there, but I always went there anyway. But this is more dramatic.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Seems the OP is getting the same type of answer there Bob.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    Chrome is blocking me also when I try to access excelforum. It says malware.
    Anyone knows what the problem is?

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It appears to be fixed now.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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