Use Lenovo's back and forward keys to switch excel tabs - excel

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

Related

Move the cursor to the right next cell, rather than the cell underneath

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 want to auto-run my macro when opening the excel file

I want to auto-run this private sub when opening the excel sheet.
I tried using Private Sub Workbook_Open() method but as the first private sub does not have a name, it does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
'
' HideF Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG()
'
' HideFG Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
I hope that it automatically checks cell W16 when opening the excel file and carries on with HideF macro or HideFG macro. Currently, the two macros run once you actual type on the cell after opening the file.
the easiest way is to use the default Module "ThisWorkbook" which gets executed when opening the excel file. You can find it within your VBA Project Explorer on the left side of the window.
Just take the sub you want to execute and copy it into the space.
Its explained in great detail here:
https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
If it is necessary for your usecase this can help you to call a private sub:
Private Sub PrivateCallDemo()
'Module2
Application.Run "Module1.Worksheet_Change"
End Sub
This way your actual Sub could stay in another Module.
You have a few problems. First you don't want Worksheet_Change(ByVal Target As Range)
as that is for events triggers on changes to the workbook, you want Workbook_Open(). This gets stored under ThisWorkbook not a separate module/sheet.
Here is working code, I commented out your ws declaration for testing.
Private Sub Workbook_Open()
'Dim ws As Worksheet: Set ws = Sheets("Budget- Reporting")
If Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End Sub
Sub HideF()
MsgBox "HideF"
End Sub
Sub HideFG()
MsgBox "HideFG"
End Sub
Here is a screenshot of my editor.
G.M. posted a great resource as well found here --> https://support.office.com/en-us/article/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44
I just put the modules in the same spot for the screenshot, but you can put them separately and still use the Call HideFG method if you want to store your modules separately from the workbook_open event as I would want to.

VBA to change Excel cursor for Workbook on Workbook_Open

Is there any way to change Excel cursor for specific Workbook? Can't find any solution for this.
My Workbook is locked for editing cells, only certain cells are available and I want to replace this Excel cursor:
By pointer:
I have went through this but don't understand is it possible to implement it in Workbook?
https://learn.microsoft.com/en-us/office/vba/api/excel.application.cursor
Private Sub Workbook_Open()
Application.Cursor = xlDefault
End Sub
In ThisWorkbook code module, you could do something like this. The idea here is that we capture the initial cursor style when the workbook is opened (the Workbook_Open event calls on changeCursor which stores the current cursor style in the cursor variable. Then we add some more event handlers so that when the workbook loses focus (Workbook_Deactivate) and before it closes (Workbook_BeforeClose) we restore that cursor to its previous style via the resetCursor method. There may be additional edge cases that I haven't considered, but this is the general idea you'll need to implement.
Option Explicit
Private cursor As Long
Private Sub Workbook_Activate()
changeCursor
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
resetCursor
End Sub
Private Sub Workbook_Deactivate()
resetCursor
End Sub
Private Sub Workbook_Open()
changeCursor
End Sub
Private Sub changeCursor()
cursor = Application.cursor
Application.cursor = xlNorthwestArrow
End Sub
Private Sub resetCursor()
On Error Resume Next
Application.cursor = cursor
If Err.Number <> 0 Then
Application.cursor = xlDefault
End If
End Sub

How to run code with a keystroke (not button)?

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

Excel VBA close current workbook Error 1004

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

Resources