Question

I am facing a problem in my excel workbook. I have 25+ sheets in my workbook and I want to look to sheet1 time to time. Is their any way that I can freeze first two sheets of my workbook?

Currently I am navigating through sheets by pressing ctrl+page up button. FYI I am Using MS-Office 2007

Was it helpful?

Solution

If I have understood well: you want the users stay only on Sheet1 & 2:
In the Main event:

Private Sub Workbook_Open()
    ActiveWindow.DisplayWorkbookTabs = False
End Sub

and in the Event:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If ActiveWindow.DisplayWorkbookTabs Then ActiveWindow.DisplayWorkbookTabs = False
    If ((Sh.Name) <> "Sheet1") And ((Sh.Name) <> "Sheet2") Then Sheets("Sheet1").Select
End Sub

When open disable Tabs. If people show if you try to change the code return to Sheet1.
Ad Password to VBA macro ...
If is only for quick change remove the code of Tabs...

OTHER TIPS

This code (in the ThisWorkbook module) will keep Sheet1 just to the left of whatever sheet you're on.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        
    Application.EnableEvents = False
    
    If Sh.Name <> Sheet1.Name Then
        Sheet1.Move Sh
        Sh.Activate
    End If
    
    Application.EnableEvents = True
        
End Sub

It's a little weird pressing Ctrl+PgUp to navigate a bunch of sheets because it now takes two Ctrl+PgUps to move one sheet - one moves you onto Sheet1 (because it's always to the left) and the second moves you to the next sheet (which then moves Sheet1 to the left of it).

Maybe you could build in a timer so it only moves sheet1 if you've been on a sheet for a couple of seconds.

Update Using a timer

In a standard module:


Public gshToMove As Object
Public gdtTimeToMove As Date

Sub MoveSheet()
    
    Application.EnableEvents = False
    
    Sheet1.Move gshToMove
    gshToMove.Activate
    
    Set gshToMove = Nothing
    gdtTimeToMove = 0
    
    Application.EnableEvents = True
    
End Sub

In the ThisWorkbook module

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
    If Sh.Name <> Sheet1.Name Then
        'if something's schedule, unschedule it
        If gdtTimeToMove <> 0 Then
            Application.OnTime gdtTimeToMove, "MoveSheet", , False
        End If
        
        'schedule the sheet move for three seconds from now
        gdtTimeToMove = Now + TimeSerial(0, 0, 3)
        Set gshToMove = Sh
        Application.OnTime gdtTimeToMove, "MoveSheet", , True
    End If
    
    Application.EnableEvents = True
    
End Sub

You still get a little flash when the code actually runs.

In each sheet

Private Sub Worksheet_Activate ( )
    Call Funciona
End Sub

In Module

Sub Funciona()
    With ActiveSheet
        If .Index > 1 Then
            If .Previous.Name <> "Principal" Then
                Sheets("Principal").Move Before:=ActiveSheet
            End If
        End If
    End With
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top