How to hide headings for inactive sheet, vba? - excel

I need to hide headings for inactive sheet in the same workbook. The below code works only for active sheet
ActiveWindow.Displayheadings = False
I tried to replace activewindow with sheet name but not works

Displayheadings is a Window property, so it will not work for a sheet.
Use the Workbook_SheetActivate event in the next way:
a. Declare a Boolean variable on top of ThisWorkbook code module (on the declarations side):
Public isAdmin As Boolean
b. Copy the next event code in ThisWorkbook code module:
Private Sub Workbook_SheetActivate(ByVal sh As Object)
If Not isAdmin Then
ActiveWindow.DisplayHeadings = False
Else
ActiveWindow.DisplayHeadings = True
End If
End Sub
Making the other sheets window Displayheadings = False is abstract, since you cannot see how the headings look in a not activated one...
c. In order to change the isAdmin you may try something like this:
Sub makeAdmin()
If Application.userName = "your name" Then
isAdmin = True
Else
isAdmin = False
End If
End Sub
This sub may be called from Workbook_Open event or from anywhere in your code...

Related

Hide and Unhide a range of rows based on the value in a cell

Very Basic I am sure but I can't figure it out for the life of me.
I have a set of radio buttons that changes the value of cell("L37") between 1 and 2.
I have tried to write the VBA code several different ways without luck. Please advise.
Sub hide_sheet()
If Worksheets("Feedback").Range("L37").Value = 1 Then
Rows("63:93").EntireRow.Hidden = True
Else Worksheets("Feedback").Range("L37").Value = 2 Then
Worksheets("Feedback").Rows("63:93").EntireRow.Hidden = False
End If
End Sub
I had it working perfectly fine via a Macro tied to the radio buttons; however, due to me needing to protect the sheet I needed to change it.
I am using a protect/unprotect VBA code and would like to include it into the code so it won't set off the macro/sheet protected warning.
Here is my protect/unprotect code I am using for everything.
Sub unprotect()
Worksheets("Feedback").unprotect
End Sub
Sub protect()
Worksheets("Feedback").protect , _
AllowFormattingCells:=True, _
AllowFormattingRows:=True
End Sub
Any advice would be greatly appreciated. I Thank You in Advance of your assistance.
Show/Hide Rows
Issues
The Feedback worksheet is not qualified so if the wrong workbook is active, it will fail. To reference the workbook containing this code, you can use ThisWorkbook:
ThisWorkbook.Worksheets("Feedback")...
You are using Rows("63:93") instead of Worksheets("Feedback").Rows("63:93") in the If clause. If the wrong worksheet is active (selected), it will fail.
You are using Else instead of ElseIf.
You can use the With statement to reduce typing as illustrated in the following code.
If you convert the cell value to a string, then if the cell accidentally contains an error value, the code will not fail.
The Code
Sub ShowHideRowsFix()
With ThisWorkbook.Worksheets("Feedback")
.Unprotect
Select Case CStr(.Range("L37").Value)
Case "1"
.Rows("63:93").Hidden = True
Case "2"
.Rows("63:93").Hidden = False
Case Else
End Select
.Protect AllowFormattingCells:=True, AllowFormattingRows:=True
End With
End Sub
An Improvement
To automate this operation (no need for buttons), in the sheet module of the Feedback worksheet identified in the VBE Project explorer window by e.g. Sheet1(FeedBack) (double-click to open), you could use the following code.
Private Sub Worksheet_Change(ByVal Target As Range)
Const CellAddress As String = "L37"
Dim Cell As Range: Set Cell = Me.Range(CellAddress)
If Intersect(Cell, Target) Is Nothing Then Exit Sub
ShowHideRows Cell
End Sub
Sub ShowHideRows(ByVal Cell As Range)
With Cell.Worksheet
.Unprotect
Select Case CStr(Cell.Value)
Case "1"
.Rows("63:93").Hidden = True
Case "2"
.Rows("63:93").Hidden = False
Case Else
End Select
.Protect AllowFormattingCells:=True, AllowFormattingRows:=True
End With
End Sub
I prefer the simplest approach where possible. This is the approach I use to hide columns re-written for your needs.
The following code may exist on the UserForm or in a standard Code Module:
Sub rwControl(ByRef hType As String)
Dim rwVis As Boolean
If hType = "hRW" Then rwVis = True
If hType = "uRW" Then rwVis = False
With ThisWorkbook.Worksheets("Feedback")
.Unprotect
.Rows("63:93").Hidden = rwVis
.Protect
End With
End Sub
You may call this code directly from you RadioButton_Change() Event vy including the following code:
IF RadioButton1.Value = True then 'Assuming the value is 1
rwControl "hRW" 'This hides the Rows
ELSE
rwControl "uRW" 'This unhides the Rows
End If
OR, to keep it really simple:
in your RadioButton_Change() event simply add:
Private Sub RadioButton1 Change()
With ThisWorkBook.Worksheets("Feedback")
.Unprotect
If RadioButton1.Value = True Then
.Rows("63:93").Hidden = True
Else
.Rows("63:93").Hidden = False
End If
.Protect
End With
End Sub
Using this approach negates the need for Worksheet Module Coding and Case Coding and allows you to keep the RadioButtons if you deem them important to the functionality.

How to remain Combobox focused while typing in it in excel VBA

Scenario
I have a combobox named customerGroup whereby once there is a change some other routines will be called.
Codes
Private Sub customerGroup_Change()
Application.ScreenUpdating = False
Call populateDependentCombobox
Application.ScreenUpdating = True
End Sub
Problem
When I type in this combobox (Instead of scrolling through the list by mouse), its focus changing to the workbook before I actually get correct value. Because of this, part of the typing happening directly on the workbook
Is there any way whereby I can keep the focus on the combobox itself? I tried the following way. But it is not working
Private Sub customerGroup_Change()
Application.ScreenUpdating = False
customerGroup.SetFocus
Call populateDependentCombobox
customerGroup.SetFocus
Application.ScreenUpdating = True
End Sub

Protect a sheet from opening within a workbook

I have an excel workbook with multiple sheets. Is there any way to password protect users from even opening a sheet within the workbook? The sheet has a large graph on it that I don't want everyone to be able to see, let alone edit.
Thanks in advance for your help
EDIT
I used the following code to allow users to click a formcontrol button to access the sheet in question.
Sub ShowHeatMap()
Dim S As String
S = InputBox("Enter Password")
If S = vbNullString Then
Exit Sub
ElseIf S <> "wiretransfer" Then
Exit Sub
Else
Worksheets("Training Heat Map").Visible = xlSheetVisible
End If
End Sub
This is associated with my button on a kind of "homepage" sheet that I added to the workbook.
But I can't get the sheet to remain hidden when you open the workbook again. I tried this code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Training Heat Map").Visible = xlSheetVeryHidden
End Sub
Any ideas? This code is inputted on the module for the sheet under general declarations
Via this answer, I think this might do the trick for you. Within VBA, put this within ThisWorkbook:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheets As String, Response As String
MySheet = "Sheet1"
If ActiveSheet.Name = MySheet Then
ActiveSheet.Visible = False
Response = InputBox("Enter password to view sheet")
If Response = "MyPass" Then
Sheets(MySheet).Visible = True
Application.EnableEvents = False
Sheets(MySheet).Select
Application.EnableEvents = True
End If
End If
Sheets(MySheet).Visible = True
End Sub
Obviously will require a bit of tailoring to your needs. Remember that you will also need to password protect your VBA code, otherwise anyone will be able to view it and find out the password.

Unable to set hidden Property, Protected Worksheet

I have some VBA code that protects 3 sheets in a single workbook on opening.
Private Sub workbook_open()
Sheets("InputForm").Protect "password", UserInterfaceOnly:=True
Sheets("Invoice").Protect "password", UserInterfaceOnly:=True
Sheets("List").Protect "password", UserInterfaceOnly:=True
End Sub
i also have some code that shows/hides rows based on whether TRUE/FALSE in a cell N14
Private Sub Worksheet_Calculate()
If ActiveSheet.Name = "InputForm" Then
Application.EnableEvents = False
Sheets("Invoice").Rows("57:123").Hidden = Range("N14").Value
Sheets("InputForm").Rows("57:94").Hidden = Range("N14").Value
Application.EnableEvents = True
Else
Application.EnableEvents = False
Application.EnableEvents = True
End If
End Sub
When i use a command button to execute some copy/paste code, i get an Error 1004, unable to set the hidden property of the range class. Debugging points to the Sheets("Invoice")... line.
However, if i end the debugger and click the button again, it seems to work fine...?
Any help would be greatly appreciated
You need to un-protect your sheets before you can make changes to them - even with VBA - it is the same concept.

Disable Insert Worksheet

Is there a way to prevent people from inserting worksheets manually. I have a form that creates the worksheets automatically based on the information the user puts in it. The code I currently uses will prevent people from creating worksheets but it also prevents my form from creating worksheets. Here is what I am using. It is in the workbook module.
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
End Sub
Declare this in a Module
Public BoolAdd As Boolean
This in your Workbook_NewSheet
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If BoolAdd = False Then
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
Else
BoolAdd = False
End If
End Sub
and set the BoolAdd to TRUE in your userform before you add the sheet.
Private Sub CommandButton1_Click()
BoolAdd = True
Sheets.Add
End Sub
Logic: The userform will set the public variable to True. This will ensure that sheet deletion code will not run. Also we have to set it back to false else the user will be able to add the sheet after the userform is closed.

Resources