next sheet macro
Find bottlenecks in your Excel workbooks
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: next sheet macro

  1. #1
    New Member
    Join Date
    Mar 2002
    Location
    Iceland
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    ... To begin with.. I am a total beginner.

    I want to make two macros that I can assign to buttons. One should activate the next sheet in the workbook and the other button should activate the previous sheet.



    thanks

  2. #2
    MrExcel MVP Jay Petrulis's Avatar
    Join Date
    Mar 2002
    Location
    Chicago, IL USA
    Posts
    2,040
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Gaur,

    The following will move to the next or previous sheets in the workbook. This replicates clicking on the tabs for the sheet.

    ---begin VBA---
    Sub next_sheet()
    Dim wkshtcount As Integer
    wkshtcount = ThisWorkbook.Sheets.Count
    If ActiveSheet.Index <> wkshtcount Then
    Sheets(ActiveSheet.Index + 1).Activate
    End If
    End Sub

    Sub previous_sheet()
    If ActiveSheet.Index <> 1 Then
    Sheets(ActiveSheet.Index - 1).Activate
    End If
    End Sub
    ---end VBA---

    Regards,
    Jay

  3. #3
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Same thing only different:

    Code:
    sub pre()
    ActiveSheet.Previous.Select
    end sub
    Code:
    sub nxt()
    ActiveSheet.Next.Select
    end sub


  4. #4
    Rest in Peace
    Join Date
    Feb 2002
    Posts
    1,582
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Gaur

    Just a quick ammedment to Nates

    Sub pre()
    On Error Resume Next
    ActiveSheet.Previous.Select
    On Error GoTo 0
    End Sub

    Sub Nxt()
    On Error Resume Next
    ActiveSheet.next.Select
    On Error GoTo 0
    End Sub


    Just to stop any Run-time errors if you are on the last or first sheet.



  5. #5
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Right-o Dave, nice catch, and I apologize for the sloppy code. Might want to have the end-user catch some grief as below.

    Code:
    Sub pre() 
    On Error goto errorhandler
    ActiveSheet.Previous.Select 
    exit sub
    errorhandler:
    msgbox "Yer already on the first worksheet!"
    End Sub
    Code:
    Sub Nxt() 
    On Error goto errorhandler
    ActiveSheet.next.Select 
    exit sub
    errorhandler:
    msgbox "Yer already on the last worksheet!"
    End Sub
    Incidentally, in all seriousness, nice site Dave.

    _________________
    Cheers, NateO

    [ This Message was edited by: NateO on 2002-03-20 19:42 ]

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com