How to Freeze sheets in Excel - excel

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

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...

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

Related

Combo box to switch between hidden sheets

I'm trying to make a combo-box that is able to switch between sheets in a workbook. for simplicity I want these sheets to be hidden.
I currently have this to switch between tabs but its unable to work with hidden sheets. assuming I'm going to have to unhide and hide the sheets while switching for this to work. just not sure how to go about that, as everything I have tried has not worked.
Private Sub ComboBox1_Change()
If ComboBox1.ListIndex > -1 Then Sheets(ComboBox1.Text).Select
End Sub
Private Sub ComboBox1_DropButtonClick()
Dim xSh As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
If ComboBox1.ListCount <> ThisWorkbook.Sheets.Count Then
ComboBox1.Clear
For Each xSh In ThisWorkbook.Sheets
ComboBox1.AddItem xSheet.Name
Next xSh
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Private Sub ComboBox1_GotFocus()
If ComboBox1.ListCount <> 0 Then ComboBox1.DropDown
End Sub
You just need to interact with the Worksheet.Visible property of the sheets.
In your ComboBox1_Change event, when you're going to switch to the selected sheet, check the property and if not visible, make it visible before trying to switch to it.
Private Sub ComboBox1_Change()
If ComboBox1.ListIndex > -1 Then
With Sheets(ComboBox1.Text)
If .Visible <> xlSheetVisible Then .Visible = xlSheetVisible
.Select
End With
End If
End Sub
You may also want to have something to make the previous sheet re-hidden again. To do so, just set the .Visible property back to xlSheetHidden (regular) or xlSheetVeryHidden (hides it so that the user cannot make the object visible, even if they dig into the menus).

Workbook_BeforeClose - Standard saving form pops out but error 91 when clicking on "Cancel"

I would like to write a code which, before closing the Workbook, sets all the sheets except one cover as very hidden.
I click on the "X" to close the Workbook, the Macro is fired and everything fine.
Then I receive the classic saving form of Excel and, if I click cancel, I receive error 91 - Object variable or With block variable not set.
Could someone explain me why is happening? I used the same code in the past and I did not have this issue
It is interesting because, if there is another excel workbook open at the same time, it works everything fine.
In Tab ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = True
Call my_macro 'defined in a separate module
Application.EnableEvents = False
End Sub
For the sake of clarity in Module 1 the code is following:
Public Sub my_macro()
Application.ScreenUpdating = False
On Error GoTo skip
Dim ws As Worksheet
Sheet8.Visible = True
For Each ws In Worksheets
If ws.Name = "Cover" Then
Else
ws.Visible = xlSheetVeryHidden
End If
Next ws
Sheet8.Select
Range("A1").Select
Application.ScreenUpdating = True
ActiveSheet.EnableSelection = xlNoRestrictions
Application.EnableEvents = True
skip:
Application.EnableEvents = True
End Sub

How to sync All Textbox1 in Workbook?

My workbook has five sheets with text boxes.
Sheet1, Sheet2, ......, and Sheet5.
And the code below is on every sheet.
Private Sub TextBox1_Change()
If Len(TextBox1.Value) = 0 Then
ActiveSheet.AutoFilterMode = False
Else
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
ActiveSheet.Range("A2:C" & Rows.Count).AutoFilter field:=1, Criteria1:="*" & TextBox1.Value & "*"
End If
End Sub
I would like to synchronize the TEXTBOXES on all sheets.
For example, if I type text in TEXTBOX1 of Sheet1, the same text will be entered in TEXTBOX1 of all other sheets.
And I also want to know how to clear TEXTBOX on every sheet at once.
1) put the following code in new Module of your project
Public dontDoThat As Boolean ' a public variable, visible throughout all your project you'll use to give way to synchronizing activity
Option Explicit
Sub Synchronize(txt As String, shtName As String)
dontDoThat = True ' set your public variable to True and prevent subsequent TextBox1_Change() events to run it again
Dim sht As Variant
For Each sht In Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")
If sht <> shtName Then Worksheets(sht).TextBox1.Text = txt
Next
dontDoThat = False ' set your public variable to False and allow subsequent TextBox1_Change() events to run it
End Sub
2) change your TextBox1_Change() event in all your sheets as follows
Private Sub TextBox1_Change()
If Not dontDoThat Then Synchronize Me.TextBox1.Text, Me.Name 'call Synchronize() only if your public variable allows it to
...
(rest of your previous code follows)
...
End Sub
To clear all textboxes just clear one of them
You can add module in your project and add this Sub there (adjusting names of sheets and TextBoxes, if needed):
Sub SetText(txt As String)
Worksheets("Sheet1").TextBox1.Text = txt
Worksheets("Sheet2").TextBox1.Text = txt
Worksheets("Sheet3").TextBox1.Text = txt
Worksheets("Sheet4").TextBox1.Text = txt
Worksheets("Sheet5").TextBox1.Text = txt
End Sub
Then, in Change event for every TextBox add this
Private Sub TextBox1_Change()
SetText Me.TextBox1.Text
End Sub
To clear all text you can set .TextBox1.Text = ""

How do I allow only myself to view a spreadsheet but everyone else can only a userform?

Trying to work out how only I can see the work book and everyone else can see the userform, based on network username.
Here's what I've got so far:
Private Sub Workbook_Open()
Application.Visible = False
UtilitiesReportingTool.Show
End Sub
You'll need a couple of things to stop your users looking at your worksheets - and these can easily be broken with a little knowledge.
Create a sheet with big text saying "Please enable macros"
Very hide all sheets except the macro enable sheet.
Create these two procedures in a normal module
Sub StartUp()
Dim wrkSht As Worksheet
If Environ("username") <> "CAES_MATT" Then
Application.Visible = False
UtitlitiesReportingTool.Show
Else
For Each wrkSht In ThisWorkbook.Worksheets
wrkSht.Visible = xlSheetVisible
Next wrkSht
End If
End Sub
Sub ShutDown()
Dim wrkSht As Worksheet
For Each wrkSht In ThisWorkbook.Worksheets
Select Case wrkSht.CodeName
Case "Sheet1" 'This should really be shtMacroEnable or something.
wrkSht.Visible = xlSheetVisible
Case Else
wrkSht.Visible = xlSheetVeryHidden
End Select
Next wrkSht
End Sub
In your Workbook_Open event add this code:
Private Sub Workbook_Open()
StartUp
End Sub
In your UserForm_Terminate event add this:
Private Sub UserForm_Terminate()
Application.Visible = True
ShutDown
End Sub
When you open the workbook only the 'Enable Macros' sheet will be visible if macros aren't enabled.
When macros are enabled the form is displayed, unless it's you in which case all the sheets are unhidden.
When the form is closed everything except the sheet with codename Sheet1 is hidden.
I may have missed something ... heading home.

Creating multiple Macro buttons to show/hide specific worksheets

New here and I just started to teach myself coding. I have a workbook that has roughly 14 tabs/worksheets for employees to enter their hours worked per day. On a "Summary" tab and want to create a macro button for each employee to click on to view his/her tab. These employee tabs are hidden and all I want the action to do is unhide and then hide when the employee clicks their button.
Unfortunately, I receive an Ambiguous Error message and I created a module per employee. I assume I need to somehow "stack" code, but again am totally new to coding. Below is a sample of my code
Private Sub ShowHideWorksheets()
Sheets("EMPLOYEE 1").Visible = Not Sheets("EMPLOYEE 1").Visible
End Sub
you need to correctly put it behind a button. When you insert the button into the page, right click it and assign macro. The code would look like
Sub Button1_Click()
Sheets("EMPLOYEE 1").Visible = Not Sheets("EMPLOYEE 1").Visible
End Sub
Basically you wish to toggle visibility for a worksheet.
Assuming that you know which Sheet is going to be triggered, it is something like this:
Public Sub TriggerSheetVisibility(worksheetname as string)
Dim ws as WorkSheet
On Error Resume Next 'To avoid subscript out of range error if a worksheetname is passed that doesn't exit
Set ws = Worksheets(worksheetname)
On Error Goto 0
If Not ws Is Nothing Then 'Only when the worksheet exists, we can execute the rest of this sub:
If ws.Visible = True then
ws.Visible = False
Else
ws.Visible = True
End If
End If
End Sub
Also see https://msdn.microsoft.com/en-us/library/office/ff197786.aspx
This is also an acceptable approach? Prolly long winded though
Private Sub CommandButton1_Click()
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Sheets
If sheet.Name <> CommandButton1.Caption Then
sheet.Visible = False
End If
If sheet.Name = CommandButton1.Caption Then
sheet.Visible = True
End If
Next sheet
End Sub
However I like this better due to the fact you only need one button
Private Sub CommandButton1_Click()
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Sheets
If sheet.Name <> Environ("USERNAME") Then
sheet.Visible = False
End If
If sheet.Name = Environ("USERNAME") Then
sheet.Visible = True
End If
Next sheet
End Sub
If you change Private to Public it should work. I'm presuming you're just creating macros at this point to get base functionality to work. You can hide (as the code you've posted) and unhide like this:
' This first macro actually just makes the worksheet visible and then
' invisible each time you execute it - so I'm not sure if
' that's what you're after
Public Sub ShowHideWorksheets()
Sheets("EMPLOYEE 1").Visible = Not Sheets("EMPLOYEE 1").Visible
End Sub
' If it's invisible you can do this.
Public Sub ShowWorksheets()
Sheets("EMPLOYEE 1").Visible = True
End Sub
' Basically that should give you an idea of how to proceed.

Resources