none
Change action for TAB key RRS feed

  • Question

  • Good day all-

    I'm curious if I can set VBA code (for just one worksheet within my workbook) to modify the TAB key such that instead of moving the cursor to the right, it moves the cursor down.

    Does anyone know how this would/could be done?

    Thanks!

    Casey

    Friday, February 12, 2016 2:47 PM

Answers

  • I'm curious if I can set VBA code (for just one worksheet within my workbook) to modify the TAB key such that instead of moving the cursor to the right, it moves the cursor down.

    Sure, we can execute a macro for any key, so we can do almost anything by a keystroke.

    Copy the code below into the code module of that sheet, switch to another sheet and back.

    There is one issue left, when you save the file with this special sheet active, the Worksheet_Activate isn't executed when the file is reopened.

    So you have to check in Workbook_Open if that sheet is active and call that sub from there, e.g:

    Private Sub Workbook_Open()
      If ActiveSheet.Name = "WhatEver" Then ActiveSheet.Worksheet_Activate
    End Sub
    Andreas.
    Sub Worksheet_Activate()
      Application.OnKey "{TAB}", Me.CodeName & ".TabMoveDown"
    End Sub
    
    Private Sub Worksheet_Deactivate()
      Application.OnKey "{TAB}"
    End Sub
    
    Public Sub TabMoveDown()
      SendKeys "{DOWN}"
    End Sub

    Saturday, February 13, 2016 5:31 AM
  • Good day all-

    I'm curious if I can set VBA code (for just one worksheet within my workbook) to modify the TAB key such that instead of moving the cursor to the right, it moves the cursor down.

    Does anyone know how this would/could be done?

    Thanks!

    Casey

    Hi Casey,

    This is how I've done it.

    In your VBA editor, right click the sheet1 and select View Code

    Enter the following code...

    Sub Worksheet_SelectionChange(ByVal Target As Range)
        Application.OnKey "{TAB}", "movedown"
    End Sub

    Now right click the Microsoft Excel Objects area and insert a module.

    Copy and paste the following code

    Public Sub movedown()
        Sheet1.Activate
        ActiveCell.Offset(1, 0).Select
    End Sub

    Now every time you use the Tab key on sheet1, the cursor will move down instead of across.

    Let me know how you go.

    Cheers

    Brad


    Sunday, February 14, 2016 2:11 PM

All replies

  • Hi

    I'm not sure what your trying to do but this event macro will do it only if you enter something in the cell otherwise if you just press Tab, it goes to the right.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If vbKeyTab Then ActiveCell.Offset(1, -1).Select
    End Sub


    Cimjet

    • Proposed as answer by Soliddrew Monday, February 15, 2016 8:56 AM
    Saturday, February 13, 2016 3:47 AM
  • I'm curious if I can set VBA code (for just one worksheet within my workbook) to modify the TAB key such that instead of moving the cursor to the right, it moves the cursor down.

    Sure, we can execute a macro for any key, so we can do almost anything by a keystroke.

    Copy the code below into the code module of that sheet, switch to another sheet and back.

    There is one issue left, when you save the file with this special sheet active, the Worksheet_Activate isn't executed when the file is reopened.

    So you have to check in Workbook_Open if that sheet is active and call that sub from there, e.g:

    Private Sub Workbook_Open()
      If ActiveSheet.Name = "WhatEver" Then ActiveSheet.Worksheet_Activate
    End Sub
    Andreas.
    Sub Worksheet_Activate()
      Application.OnKey "{TAB}", Me.CodeName & ".TabMoveDown"
    End Sub
    
    Private Sub Worksheet_Deactivate()
      Application.OnKey "{TAB}"
    End Sub
    
    Public Sub TabMoveDown()
      SendKeys "{DOWN}"
    End Sub

    Saturday, February 13, 2016 5:31 AM
  • Sub Worksheet_Activate()
      Application.OnKey "{TAB}", Me.CodeName & ".TabMoveDown"
    End Sub
    
    Private Sub Worksheet_Deactivate()
      Application.OnKey "{TAB}"
    End Sub
    
    Public Sub TabMoveDown()
      SendKeys "{DOWN}"
    End Sub
     Does the above code work?
      Application.OnKey Method (Excel) says that with "Application.OnKey", the first argument must be combined with ALT, CTRL, or SHIFT.


    • Edited by Ashidacchi Sunday, February 14, 2016 4:30 AM
    Sunday, February 14, 2016 4:29 AM
  •  Does the above code work?
      Application.OnKey Method (Excel) says that with "Application.OnKey", the first argument must be combined with ALT, CTRL, or SHIFT.

    a) It would be nice if you try the code the next time before you ask. Then you can answer the question by yourself.

    b) The article did not contain the word "must" anywhere!


    That means it is possible to combine a keystroke with ALT, CTRL and/or SHIFT.

    Andreas.

    Sunday, February 14, 2016 9:23 AM
  • Hi Andreas Killer,

    I apologize you and thank you.
    I've misread the article and tried your code now. It works file. 

    VBA could handle key-event. I felt educated.
    Thanks again.

    Best wishes.
    Sunday, February 14, 2016 9:35 AM
  • Good day all-

    I'm curious if I can set VBA code (for just one worksheet within my workbook) to modify the TAB key such that instead of moving the cursor to the right, it moves the cursor down.

    Does anyone know how this would/could be done?

    Thanks!

    Casey

    Hi Casey,

    This is how I've done it.

    In your VBA editor, right click the sheet1 and select View Code

    Enter the following code...

    Sub Worksheet_SelectionChange(ByVal Target As Range)
        Application.OnKey "{TAB}", "movedown"
    End Sub

    Now right click the Microsoft Excel Objects area and insert a module.

    Copy and paste the following code

    Public Sub movedown()
        Sheet1.Activate
        ActiveCell.Offset(1, 0).Select
    End Sub

    Now every time you use the Tab key on sheet1, the cursor will move down instead of across.

    Let me know how you go.

    Cheers

    Brad


    Sunday, February 14, 2016 2:11 PM