Drop-down list of hyperlinks to different sheets in Excel - excel

I have 5 different sheets: S1, S2, S3, S4, S5. And an "Info" sheet where i have the overall information, where in every row I want a dropdown along with direct link to one of the sheets(S1, S2, S3, S4, S5). I tried using Indirect function along with Address but it is not dynamic. Is there a way of how to make a drop-down list with hyperlink to the sheet directly inbuilt with that?
I hope i could describe my situation.

No need to re-invent the wheel. This is already built into Excel. Righ-click the sheet navigation arrows in the bottom left hand corner and the list of sheets will pop up. Click the sheet you want to go to. Hidden sheets (like Sheet3 in my screenshot) will no be listed.

Place a drop-down in cell, say, A5.
In B5 enter:
=HYPERLINK("#" & A5 & "!A1",A5)
This will create a "hot" hyperlink to cell A1 of the sheet you picked in the drop-down:
EDIT#1:
Place your drop-downs in column A. Each drop-down can select any worksheet. Then in the worksheet code area enter:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, sht As String, sh As Worksheet
Set A = Range("A:A")
If Intersect(Target, A) Is Nothing Then Exit Sub
sht = Target.Value
For Each sh In Sheets
If sh.Name = sht Then
sh.Activate
End If
Next sh
End Sub
whenever you change a value in column A, the code checks it the new value is a valid sheet-name. If it is, the code jumps to that sheet. If it is not a valid name, nothing bad happens.
This approach has the advantage that if you add/remove worksheets, no code change is required.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Related

Automatically update a cell in Excel with the current date anytime any cell in a column is updated or changed

Not sure if this is possible in Excel, but I would like cell K9 to be updated with today's date every time ANY cell in K11:K119 is changed. Some cells in K11:K119 are blank, but can have data entered into them at any time. Cells in K11:K119 that already have data could change. So I would like cell K9 to update with today's date any time any single blank cell in K11:K119 is updated with data, or any time any single cell in K11:K119 with data is changed. Hope that makes sense.
Place the following Event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range
Set r1 = Range("K9")
Set r2 = Range("K11:K119")
If Not Intersect(Target, r2) Is Nothing Then
Application.EnableEvents = False
r1.Value = Date
Application.EnableEvents = True
End If
End Sub
The macro monitors your changes to the worksheet. If you change any value in the input range, the cell with the date is refreshed.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
If you don't want to enable macros, try something simpler:
=IF(COUNT(K11:K119)>=0,TODAY(),0)
COUNT(range) will always be ≥0 (so it doesn't matter what you put as the third argument) and it always updates when any member of the range is changed.

Is there a Macro to hide all sheets in a workbook when a certain cell is equal to 100%?

I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub

Is it possible to have a cell which contains a formula and be able to input text into the same cell?

I am trying to create in excel where you enter initials into a cell and the person's full name appears automatically so they do not have to enter their full name every time in a spreadsheet. I have managed to replicate this using nested if statements for different people which works fine but currently it requires two cells and I want to have it on the same cell. I have noticed that if I change the the formula to be in the same cell then it comes up with "circular reference warning" and does not work and if I try to enter text in that cell it replaces the formula. Just wondering is this possible?
=IF(C29="HB", "hazel", IF(C29="AO", "amelia", ""))
You could use Excel's AutoCorrect feature to do this.
Go to File..Options..Proofing -
Click on AutoCorrect Options..
Enter a pair (or triplet) of initials you want to replace and the name you want the initials to be replaced with, then click add. You can then continue adding Replace With definitions.
It doesn't matter if you enter the Replace in lower case "hb" or upper case "HB" because the entry and its replacement are both added to the definitions in lower case.
Excel does this for a reason - it is sensitive to the case of the cell entry. That is to say if you enter "hb" the autocorrect will change this to "hazel"; if you enter "Hb" you will get "Hazel"; and "HB" will give you "HAZEL".
The replacement pairs you enter will only apply to the currently logged username. The replacement pairs will be there every time you open Excel and are available to all open workbooks.
If you want the replacement pairs to be available to other users you will need to:
manually add the replacement pairs to all desired users Excel Options or
develop a macro to add them using Application.AutoCorrect.AddReplacement "hb", "hazel"
If you decide to go down the macro path, you may want to limit the scope of definitions by entering them in certain workbook or worksheet events and consider using the Application.AutoCorrect.DeleteReplacement "hb" method.
Workbook_Open
The replacements will be available to the current user in all workbooks and will remain until the are deleted manually or programmatically.
Workbook_Activate and Workbook_Deactivate
If you use the AddReplacement method in Workbook_Activate and the DeleteReplacement method in Workbook_Deactivate then the replacements will only be available to sheets in the workbook containing the code.
Worksheet_Activate and Worksheet_Deactivate
Place the code in a sheet module within the above sheet event handlers and the replacements will only be available in the sheet corresponding to that module.
This is just an example that you can adapt to your needs. The initials input will be in column A so if the user types JW in a cell in that column, the cell will change to James Ravenswood.
Put the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
ary = Array("JW", "VL", "BJM")
bry = Array("James Ravenswood", "Victor Laszlo", "Bullwinkle J Moose")
Set A = Range("A:A")
If Intersect(Target, A) Is Nothing Then Exit Sub
v = Target.Value
For i = LBound(ary) To UBound(ary)
If v = ary(i) Then
Application.EnableEvents = False
Target.Value = bry(i)
Application.EnableEvents = True
Exit Sub
End If
Next i
End Sub
You would need to fill ary and bry with your initials and names.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Prevent row from being changed after initial write

I want to be able to set values in a cell but prevent the cell from being changed easily afterwards.
The cell will store a random int and so if the user edits the cell and presses enter a new random int is generated. These id's are mapped to unit tests so updates are undesirable.
How could I solve this please?
Here is a simple example for a single cell, cell B9.
It assumes that we start with all cells UNLOCKED
Enter the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim B9 As Range
Set B9 = Range("B9")
If B9.Value = "" Then Exit Sub
If Intersect(Target, B9) Is Nothing Then Exit Sub
B9.Locked = True
ActiveSheet.Protect Password:="secret"
End Sub
Once a value in entered in B9, that cell is locked and the worksheet is protected with the password secret.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

How to Protect the sheet name not the entire sheet?

I need to protect the sheet name by preventing
any change to the sheet name, or
the sheet being deleted.
This must be done without protecting the entire sheet using the Protect Sheet or Protect Workbook options.
Is there any way to do this with VBA?
Right click the sheet tab that you wish to protect
View Code
Copy and paste in the code below
This code disables the delete control on the sheet (but not right click on cell) menu when the sheet is activated. The control is enabled when the sheet is de-activated
The code will also name the sheet "NameOfSheet" when the sheet is de-activated. This is a workaround to prevent the sheet being renamed
Private Sub Worksheet_Activate()
Application.CommandBars.FindControl(ID:=847).Enabled = False
End Sub
Private Sub Worksheet_Deactivate()
Application.CommandBars.FindControl(ID:=847).Enabled = True
Me.Name = "NameOfSheet"
End Sub
I don't think you can. What you can do, you can make a worksheet a very hidden one (accessible only from VBA) and in case of the deleted sheet, you can copy it and make a copy visible.
Would this approach work?
Select all cells in the sheet, then UN-lock all cells with "Lock
Cells" (yellow background of padlock turns white).
Write the name of the sheet in a (fixed or named) cell, then lock
this cell ONLY ("Lock Cells", padlock background turns yellow).
Then Protect workbook, but allow every action, except the first one
"Select Locked Cells".
The user can do everything except selecting the cell with the sheetname (and delete rows/columns).
Now write a VBA to compare the actual sheetname with the data in the protected named cell (or fixed reference e.g. A1).
Run this script either on every change (probably too much) or at least on close of
the workbook.
As long as the sheetname is always in the same cell (e.g. A1), you can then loop through all sheets, compare their name with the data in cell A1 and correct the sheet name if required.

Resources