![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Location: Iceland
Posts: 1
|
... 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 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
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 |
|
Legend
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
|
Same thing only different:
Code:
sub pre() ActiveSheet.Previous.Select end sub Code:
sub nxt() ActiveSheet.Next.Select end sub |
|
|
|
|
|
#4 |
|
Banned
Join Date: Feb 2002
Posts: 1,582
|
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 |
|
Legend
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
|
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 _________________ Cheers, NateO [ This Message was edited by: NateO on 2002-03-20 19:42 ] |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|