I am trying to write a code using Macro, but with no luck.
The task can be simply defined as, when the user enters a Serial Number such "AB123" anywhere in column I, a list should appear automatically in columns in (J, K, L). please the the attached picture
If there is any other way to do that without using Macro, I am glad to hear it.
Thank you in advance, I hop that I make myself clear.
The pic. shows whenever the RED Serial Number is entered. The Highlighted columns in green should appear.
Something for you to think about. Put this code in your Sheet. Double-click the sheet name and a windoe to enter code will appear.
Option Explicit
Public Sub Worksheet_Activate()
Me.Range("J:L").EntireColumn.Hidden = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("I:I")) Is Nothing Then
If Target.Cells.CountLarge = 1 Then
If Target.Value = "AB1234" Then
Me.Range("J:L").EntireColumn.Hidden = False
Else
Me.Range("J:L").EntireColumn.Hidden = True
End If
Else
'Nothing
End If
End If
End Sub
Private Sub Worksheet_Deactivate()
Me.Range("J:L").EntireColumn.Hidden = False
End Sub
I am currently trying to put a hyperlink in one overarching tab that would trigger an autofilter in another tab once clicked. I've put in what i believe to be the correct macro for the hyperlink but when clicked nothing happens. I even tried it on a new Excel document and the code works fine. Is there something I could update in my document to allow hyperlinks or is my code incorrect.
Thanks!
In-Sheet code:
Private Sub Worksheet_Hyperlink(ByVal Target As hyperlink)
If Target.Range.Address = "$F$5" Then
Call hyperlink2
End If
End Sub
Reference Code:
Sub hyperlink2()
Application.ScreenUpdating = False
Worksheets("NJ Reporting Template").Activate
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Range("Z6").Select
ActiveSheet.Range("$N$6:$Z$150").AutoFilter Field:=26, Criteria1:="Yes"
Application.ScreenUpdating = True
End Sub
I've created a menu to navigate through sheets. But when I use one of the buttons to go to another sheet and type something in this another sheet the things that I type appear in the first one. Although when I use the tabs to jump to one sheet to another it works fine. Seems to be that the macro is considering a relative reference instead a absolute one.
Here's what my macro does (or at least should):
Private Sub cadastrar_clientes_Click()
Application.ScreenUpdating = False
Menu.Hide
Sheets("Clientes").Select
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Select
End Sub
Notes:
I've tried to use ".activate" instead of ".select".
I've tried to use only "Sheets("Clientes").Select", even so the bug occurs.
To execute "Excel /unregserver" on the Windows CMD solved my problem once, but I couldn't do this again. It doesn't seems to work anymore.
I did a menu like this one once, but I didn't had these problems.
When the workbook opens the following code is executed:
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
End Sub
That's it. I hope that somebody could help me.
Replace all code on VBA module Menu with this (tested in the file you provided)
Option Explicit
Private Sub fechar_menu_Click()
Menu.Hide
End Sub
Private Sub novo_pedido_Click()
goToTab ActiveWorkbook.Worksheets("Novo pedido"), "B5"
End Sub
Private Sub ver_pedidos_Click()
goToTab ActiveWorkbook.Worksheets("Consultar pedido"), "F1"
End Sub
Private Sub cadastrar_clientes_Click()
goToTab ActiveWorkbook.Worksheets("Clientes")
End Sub
Private Sub cadastrar_produtos_Click()
goToTab ActiveWorkbook.Worksheets("Produtos")
End Sub
Private Sub cadastrar_transportadoras_Click()
goToTab ActiveWorkbook.Worksheets("Transportadoras")
End Sub
Private Sub painel_financeiro_Click()
goToTab ActiveWorkbook.Worksheets("Painel Financeiro"), "B3"
End Sub
Private Sub goToTab(ByRef ws As Worksheet, Optional cel As String = vbNullString)
Menu.Hide
Application.ScreenUpdating = False
With ws
.Activate
If Len(cel) = 0 Then
.Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Select
Else
.Range(cel).Select
End If
End With
Application.ScreenUpdating = True
End Sub
Well, I guess I know a lot more about the problem now. I've tested the workbook over various computers and came to the conclusion that the problem is within the Office 2013. I haven't seem this issue when running the 2007 or 2010 version. So far I haven't come to a solution. I would like to thanks Paul Bika for helping.
I have a group of cells in my worksheet that use a data validation list. It works great, but I'd like the list to dropdown automatically when one of these cells gets the focus. I found some code online to make this work.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Worksheet.Name = "Sheet1" And Target.Column = 1 Then
Application.SendKeys ("%{down}")
End If
End Sub
Courtesy of: http://howoffice.com/automatically-open-the-drop-down-list-when-a-cell-get-focus-in-excel/
However, the issue I'm having is with the SendKeys causing the NumLock to toggle on and off. I looked this up and it's a known issue with the command.
Are there any other alternatives to the SendKey command to accomplish what I'm looking to do here?
Any help is appreciated.
This code works for me:
Courtesy of: On focus after tabbing of Excel drop down, automatically show list for selection
Private Sub Worksheet_SelectionChange(ByVal Target As Range
On Error GoTo Err1:
If Target = Range("I5") Then
Application.SendKeys ("%{UP}")
End If
If Target = Range("I6") Then
Application.SendKeys ("%{UP}")
End If
If Target = Range("I7") Then
Application.SendKeys ("%{UP}")
End If
Err1:
'do nothing
End Sub
And here's the code with SDB MadDog's corrections:
If you want it to apply to all cell's with data validation
( 'On focus after tabbing of Excel drop down, automatically show list for selection )
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target.Cells.Count = 1 Then
If Target.Validation.InCellDropdown = True Then
Application.SendKeys ("%{UP}")
End If
End If
Err1:
'do nothing
End Sub
However, it changes my NUMLOCK status (toogle ON / off) everytime!
He mentioned he fixed it with an API, I tried, but it doesn't work because it can't get numlock status read correctly (this one http://www.freevbcode.com/ShowCode.asp?ID=1151)
But hope this helps someone in need!
this code works for me :
SendKeys "%{down}", True
DoEvents
SendKeys "{SCROLLLOCK}"
Protecting workbook structure will prevent a user from deleting sheets. But how could I (using VBA) prevent a user from deleting a particular sheet I designate? I've seen examples where an active sheet is prevented from deletion by
Set mymenubar = CommandBars.ActiveMenuBar
mymenubar.Controls("Edit").Controls("Delete sheet").Visible = False
in its Worksheet_Activate event but that of course only works if the sheet is activated.
Is there a way to prevent a sheet from being deleted whether active or no?
For clarity: I'm fine with the user deleting some sheets, just not a couple of particular sheets.
So protecting workbook structure won't work.
As far as I can tell, it isn't possible to natively tag a single sheet as non-deletable; and there isn't an event that can be used to detect when a sheet is about to be deleted so the workbook can be protected preventively.
However, here is one potential workaround:
Protect workbook structure: this will, as you indicate, prevent all sheets from being deleted.
Create a "Controls" sheet. On this sheet, maintain a list of all sheet names (except those you don't want to be deletable).
If users want to delete a sheet, they will have to select its name on the Controls sheet (e.g. in a data validation drop-down menu) and press a "Delete" button. This button will call a macro that temporarily unprotects the workbook, deletes the selected sheet, and then reprotects the workbook.
Of course, the users will have to get used to this way of deleting sheets (as opposed to just right-click > Delete on the sheet's tab). Still, this isn't crazy complicated.
As for how to achieve #2 i.e. maintaining that list of sheet names, I suppose you could make use of a UDF like this one (must be called as an array formula):
Function DeletableSheetNames() As String()
Application.Volatile
Dim i As Long
Dim sn() As String
With ThisWorkbook
ReDim sn(1 To .Sheets.Count)
For i = 1 To .Sheets.Count
With .Sheets(i)
If .Name = "DataEntry1" Or .Name = "DataEntry2" Then
'Don't include it in the list.
Else
sn(i) = .Name
End If
End With
Next i
End With
DeletableSheetNames = sn
End Function
You cannot stop users to delete a particular sheet but you could use the Workbook_BeforeSave() event to prevent the workbook from being saved if a particular sheet is missing. The documentation on this event precisely shows how to allow saving a workbook only when certain conditions are met. See http://msdn.microsoft.com/en-us/library/office/ff840057(v=office.14).aspx
I can prevent a sheet from being deleted via the Worksheet_BeforeDelete Event as follows:
Private Sub Worksheet_BeforeDelete()
Call ThisWorkbook.Protect("password")
Call MsgBox("This sheet cannot be deleted.", vbExclamation)
End Sub
This protects all sheets from being deleted, however if you add some event code on the ThisWorkbook module like the following :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call ThisWorkbook.Unprotect("password")
End Sub
I will then be able to delete any other sheet as soon as it is selected.
Bear in mind, you will lose copy and paste functionality between pages due to the page unlocking when it is selected.
"there isn't an event that can be used to detect when a sheet is about to be deleted"
Since Office 2013, it is possible with the SheetBeforeDelete event.
I found this solution, similar to Dan's, on ExtendOffice.com. Put this code on the Worksheet's module:
Private Sub Worksheet_Activate()
ThisWorkbook.Protect "yourpassword"
End Sub
Private Sub Worksheet_Deactivate()
ThisWorkbook.Unprotect "yourpassword"
End Sub
When you activate the sheet in question, the whole workbook is protected, and the "Delete" option is grayed out. When you switch to any other sheet, the workbook is free again. It's subtle because you only notice the change when you go to the "safe" sheet.
Answer is by adding the following code to each of the protected sheets:
Private Sub Worksheet_Deactivate()
ThisWorkbook.Protect , True
Application.OnTime Now, "UnprotectBook"
End Sub
And the following to a Module:
Sub UnprotectBook()
ThisWorkbook.Unprotect
End Sub
Check https://www.top-password.com/blog/prevent-excel-sheet-from-being-deleted/ for credits accordingly.
The following disables the menu when you right click on tab. This stops the delete option being available.
Sub tab_rclick_off()
Application.CommandBars("Ply").Enabled = False
End Sub
The following turns the menu back on.
Sub tab_rclick_on()
Application.CommandBars("Ply").Enabled = True
End Sub
This option is simple, concise, prevents any issues with data entry/editing with protected sheets and can be called from anywhere in code, ie in conjunction with log on permissions can be given to some and not others etc. foremost yourself.
Maybe you could try to protect the structure of the workbook in SheetBeforeDelete.
See my example:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ThisWorkbook.Protect Structure:=False
End Sub
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
If Sh.Name = "Example" Then
ThisWorkbook.Protect Structure:=True
End If
End Sub
Here is another answer from mine base on the idea of #Jean-François Corbett
You can use 'Protect WB Structure' and Event 'Workbook_SheetBeforeDelete' to achieve this.
The result is that an dialog will pop up said "Workbook is protected and cannot be changed."
Private Sub zPreventWShDel(WSh As Worksheet, Protection As Boolean)
Dim zPassword As String: zPassword = ""
Dim zWB As Workbook
Set zWB = WSh.Parent
If Protection Then
zWB.Protect zPassword, Protection
Else
zWB.Protect zPassword, Protection
End If
'Stop
End Sub
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
Call zPreventWShDel(Sh, True)
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call zPreventWShDel(Sh, False)
End Sub
Do not Call the code on Sheet Deactivation like below. Because it will deactivate it. Sequence of running is Event_SheetBeforeDelete -> Event_SheetDeactivate.
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Call zPreventWShDel(Sh, False)
End Sub
I created a different approach to this.
On the sheet(s) you want protected, add this code:
Private Sub Worksheet_BeforeDelete()
ThisWorkbook.Worksheets("Unprotect Workbook").Visible = True
ThisWorkbook.Worksheets("Unprotect Workbook").Activate
ThisWorkbook.Protect
End Sub
Create the ("Unprotect Workbook") sheet and make the visibility: xlSheetVeryHidden
on the "Unprotect Workbook" sheet add a button or shape that you can assign a macro to.
on the "Unprotect Workbook" sheet, add this code:
Sub unprotectThisWorkbook()
ThisWorkbook.unprotect
ActiveSheet.Visible = xlSheetVeryHidden
End Sub
Assign the sub you added, "Sub unprotectThisWorkbook()", to the button on the "Unprotect Worksheet" sheet
When you delete the sheet you protected, the workbook is protected and it takes you to the unprotect worksheet as a notice to the user and as a way to unprotect the workbook. Once the button is clicked, the workbook is unprotected and the "unprotect sheet" is hidden again.
This will work for any sheet you want to protect.