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
Related
This is what I want to achieve with the code further down: If I use the RETURN key, the cell right next to the current cell should be selected, rather than the cell underneath. When I execute the code (i.e. I change between two different worksheets, the cursor moves from the current selected cell to the right next one. When then I use RETURN, I get the message as in the attached image: "Cannot run the macro 'PathToMacro\MacroName'!TRUE: The macro may not be available in this workbook or all macros may be disabled." What I want to achieve is this:
Change the behaviour of the RETURN key so that not the underneath cell, but the cell right to the currently selected cell is selected.
Please help with this. Thank you.
"Cannot run the macro 'PathToMacro\MacroName'!TRUE: The macro may not be available in this workbook or all macros may be disabled."
Option Explicit
Sub getNextCell()
activeCell.Offset(0, 1).Activate
End Sub
Private Sub Worksheet_Activate()
Dim activeCell As Range
Set activeCell = Selection
Application.OnKey "{RETURN}", Cells(activeCell.row, activeCell.column + 1).Select
End Sub
Private Sub Workbook_Open()
Application.OnKey "{RETURN}", Cells(activeCell.row, activeCell.column + 1).Select
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{RETURN}"
End Sub
This is the answer to my question:
The procedures are specific to the sheet.
As the sheet is entered, the cursor moves to the right
As the sheet changes to another sheet, regular behaviour is restored!
' move cursor to the right upon ENTER key
Private Sub Worksheet_Activate()
Application.MoveAfterReturnDirection = xlToRight
End Sub
' return to default behaviour
Private Sub Worksheet_Deactivate()
Application.MoveAfterReturnDirection = xlDown
End Sub
I have linked a Spinbutton to a list of cells (with different values in each cell). The Spinbutton works phenomenal, I would just like to be able to have a keyboard shortcut that would automatically change the spinbutton up or down depending on the key board shortcut. If there was a way I could call this code that would work as well.
Private Sub SpinButton1_SpinDown()
If Me.ComboBox1.ListIndex = Me.ComboBox1.ListCount - 1 Then Exit Sub
Me.ComboBox1.ListIndex = Me.ComboBox1.ListIndex + 1
End Sub
Private Sub SpinButton1_SpinUp()
If Me.ComboBox1.ListIndex = 0 Then Exit Sub
Me.ComboBox1.ListIndex = Me.ComboBox1.ListIndex - 1
End Sub
enter image description here
Let's say your Spinbutton is located in the sheet with code name Sheet1, then you could include the following in the ThisWorkbook Object code module :
Private Sub Workbook_Open()
'Set keyboard shortcuts
Application.OnKey "^%{DOWN}", "Sheet1.SpinButton1_SpinDown"
Application.OnKey "^%{UP}", "Sheet1.SpinButton1_SpinUp"
Msgbox "Keyboard shortcuts are now active."
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Reset keyboard shortcuts
Application.OnKey "^%{DOWN}", ""
Application.OnKey "^%{UP}", ""
End Sub
Reminder:
SHIFT = + (plus sign)
CTRL = ^ (caret)
ALT = % (percent sign)
More details
This way, every time you open the workbook the shortcut keys would be assigned and removed when you close the workbook. The shortcut key Ctrl+Alt+UpArrow would call the SpinUp procedure and Ctrl+Alt+DownArrow would call the SpinDown procedure.
Note that the code name of the sheet is different from the sheet name. The code name of a sheet is always on the left in the project explorer window:
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.
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
I would like to use Lenovo's back and forward keys to switch excel tabs
The keys are next to the arrows. How do I achieve that??
Edit - Changed Left to 166 and Right to 167 for the Lenovo Laptop
Under 'ThisWorkBook' use this code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.OnKey "{167}", "NextTab"
Application.OnKey "{166}", "LastTab"
End Sub
Under a new Module put this code:
Sub NextTab()
On Error Resume Next
Sheets(ActiveSheet.Index + 1).Activate
If Err.Number <> 0 Then Sheets(1).Activate
End Sub
Sub LastTab()
On Error Resume Next
Sheets(ActiveSheet.Index - 1).Activate
If Err.Number <> 0 Then Sheets(1).Activate
End Sub
I may be missing something, but no idea why you'd use the sheet selection change event to set the onkey parameters, this will only work after the user has first changed sheet selection, and won't trigger if imported to an addin/personal.
Suggest changing:
Private Sub Workbook_SheetSelectionChange
in the first macro to:
Private Sub Workbook_Open