How to disable CTRL+D short cut for a particular workbook?
I can disable with Macro's but macros will affect all the excel workbooks.
I tried,
Sub diableCtrlD()
Application.OnKey "^d", ""
End Sub
this is working fine only, but it's applying to all the workbooks.
I just want to disable to one particular document/workbook. How do I do that?
This will make it so that when you press CTRL+D it will check the workbook's name, it the workbook is Book1 it won't do anything, otherwise it will do the default fill down event.
Sub Sample()
Application.OnKey "^d", "CheckWorkbook"
End Sub
Sub CheckWorkbook()
If ActiveWorkbook.Name = "Book1" Then
Exit Sub
Else
Selection.FillDown
End If
End Sub
Or
Sub Sample()
Application.OnKey "^d", "CheckWorkbook"
End Sub
Sub CheckWorkbook()
If ActiveWorkbook.Name <> "Book1" Then
Selection.FillDown
End If
End Sub
Related
I created macros in vba but they don't work.
In the ThisWorkbook code I wrote:
Private Sub Workbook_Open()
Worksheets("Sheet1").Activate
End Sub
In the code of Sheet1 (Sheet1) I wrote:
Private Sub Worksheet_Activate()
MsgBox ("hello")
End Sub
but when I open the file nothing happens ...
If Sheet1 is already active when you open the workbook, then
Worksheets("Sheet1").Activate
does exactly nothing. Because you cannot activate what is already active.
Test it by adding 2 Sheets Sheet1 and Sheet2. Then select Sheet2 save the file and close it. Now open it again, it will run Worksheets("Sheet1").Activate and this will trigger the Worksheet_Activate properly.
Also note that MsgBox ("hello") should be without parenthesis as it does not return a value to a variable: MsgBox "hello"
An alternative solution is:
Write in a module:
Public Sub Sheet1_Activate()
MsgBox "Sheet1 Activate"
End Sub
In Sheet1 write:
Private Sub Worksheet_Activate()
Sheet1_Activate
End Sub
And in ThisWorkbook write:
Private Sub Workbook_Open()
If ThisWorkbook.ActiveSheet.Name = "Sheet1" Then
Sheet1_Activate
Else
ThisWorkbook.Worksheets("Sheet1").Activate
End If
End Sub
The idea is to have a new procedure Sheet1_Activate that takes the actual code and is triggered by the Worksheet_Activate as well as by the Workbook_Open in case the sheet is already the active sheet.
Sub auto_open()
MsgBox "HueHueHue"
End Sub
You can't activate the same thing twice.
My spreadsheet hides and shows multiple rows by clicking buttons. As the spreadsheet is password protected any macro should apply password, then runs hide/show and finally set password again. This is how its look like.
Sub Macro1()
ActiveSheet.Unprotect Password:="abc"
Rows("12:16").EntireRow.Hidden = True
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro2()
ActiveSheet.Unprotect Password:="abc"
Rows("12:16").EntireRow.Hidden = False
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro3()
ActiveSheet.Unprotect Password:="abc"
Rows("20:24").EntireRow.Hidden = True
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro4()
ActiveSheet.Unprotect Password:="abc"
Rows("20:24").EntireRow.Hidden = False
ActiveSheet.Protect Password:="abc"
End Sub
Script works fine, but I have 16 sections which require 32 macro's. It still works fine but I wonder if there would be an easier way, requiring only 1 line for applying and 1x for setting the password.
Thank you for your comments.
Dennis
The Netherlands
Sub Macro1
HideIt Rows("12:16"), True
End Sub
Sub Macro2()
HideIt Rows("12:16"), False
End Sub
Sub HideRows(rng As Range, HideIt as Boolean)
ActiveSheet.Unprotect Password:="abc"
rng.EntireRow.Hidden = HideIt
ActiveSheet.Protect Password:="abc"
End Sub
If you could name your buttons with something which would enable to to translate the names to a range and true/False, you could link them all to a single Sub and use Application.Caller to get the name of the calling button and extract the parameters from that.
EDIT:
OK here's a very simple example: add two "forms" buttons to your worksheet and name one "btn_12_5_H" and the other "btn_12_5_S".
Here's how you name each button:
Select the button via a right-click
Enter the name in the "name" box in the formula bar and press Enter
Link both buttons to the Sub below (right-click button >> Assign macro):
Sub ShowHideRows()
Dim arr
'split the calling button name into an array
' (array will be zero-based)
arr = Split(Application.Caller, "_")
'**EDIT** check array is expected size...
If UBound(arr) <> 3 Then Exit Sub
If IsNumeric(arr(1)) and IsNumeric(arr(2)) Then
With Me 'if the code is in the sheet module, else "ActiveSheet"
.Unprotect Password:="abc"
'arr(1) determines start row
'arr(2) determines # of rows
'arr(3) determines if rows are hidden or not
.Cells(arr(1), 1).Resize(arr(2), 1).EntireRow.Hidden = (arr(3) = "H")
.Protect Password:="abc"
End With
End If
End Sub
EDIT#2:
Just for completeness, note that you can also add arguments directly to the OnAction (i.e. when you right-click the button an select "Assign macro")
For example you can use something like:
Book1!'ShowHideRows2 12,TRUE'
Note use of single quotes around the whole thing. The called sub might look something like (very basic example to demonstrate that the arguments were properly passed):
Sub ShowHideRows2(rownum, HideIt)
Debug.Print rownum, HideIt
End Sub
Note that because the Sub has parameters it won't show up in the "Assign macro" list and you have to type it in.
I want to increment the value of a cell by 1 when I input a specific key combination.
I used Record Macro but when I edit that code it doesn't show how it binds the macro to the keystroke.
Here's the code I have that does not work:
Sub IncTest2()
ActiveCell.Value = ActiveCell.Value + 1
End Sub
Sub KeyTest()
Application.OnKey "^a", "IncTest2"
End Sub
It should increment the active cell by 1 with "Control+a", but instead it does the standard "Control+a" keyboard shortcut.
Try this and see if it works for you. In the ThisWorkBook code, add the sub below.
Sub Workbook_Activate()
Application.OnKey "^a", "IncrementByeOne"
End Sub
In a module in ThisWorkBook, add the macro below.
Sub IncrementByeOne()
With ThisWorkbook.ActiveSheet
ActiveCell.Value = ActiveCell.Value + 1
End With
End Sub
I'am trying to prevent users from pasting other things than values in the template I'm developing. I use a macro to always paste values in the worksheet (see below). When users switch to another workbook this macro should be disabled.
The problem is that I get error 91 when activating another workbook.
'the macro in a module
Sub AlwaysPasteValues()
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
'the code in this workbook
Public wb As Workbook
Private Sub Workbook_Activate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:="v"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set wb = Nothing
End Sub
Private Sub Workbook_Deactivate()
With wb
.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End With
End Sub
Private Sub Workbook_Open()
Set wb = ThisWorkbook
End Sub
You are changing .MacroOptions on wb which does not exist as a property. MacroOptions is for the Application. Use the same code as the Activate and you should be good.
Private Sub Workbook_Deactivate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End Sub
I have some VBA code in an Excel workbook.
I have a cell which I would like to use to close the workbook without saving (instead of using the usual (X) close button on the top right corner of excel.
If I close the workbook using the (X) button, it works great the following code.
However if I press the "CLOSE" button cell which is on the worksheet, it gives a 1004 error.
Can anyone help?
ON WORKSHEET WHERE BUTTON IS
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Cells(ActiveCell.Row, ActiveCell.Column) = "CLOSE" Then
CloseNoSave
End If
End Sub
IN THISWORKBOOK
Private Sub Workbook_BeforeClose(Cancel As Boolean)
CloseNoSave
End Sub
IN MODULE
Sub CloseNoSave()
ThisWorkbook.Close savechanges:=False
End Sub
Why don't you just use a single piece of code on your Selection Event
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "CLOSE" Then
ThisWorkbook.Saved = True
ThisWorkbook.Close
End If
End Sub