Home » excel » windows – Excel VBA – Is there a TextChanging / TextChanged or a similar event? Or how to move a selection without using Enter?

windows – Excel VBA – Is there a TextChanging / TextChanged or a similar event? Or how to move a selection without using Enter?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have recently been asked if I could make a macro in Excel VBA that will allow a user to type in two numbers and have it automatically drop to the next row. The purpose of this is so they can type in grades for a test two numbers at a time without pressing enter since they aren’t great at typing.

When I first heard this he mentioned it was Visual Basic, so I figured I’d just use a TextChanging or TextChanged event in the cell range and have it work off that. However, I haven’t been able to find any such event or anything resembling it in the documentation thus far. The first thing that I came across was Workbook_Change, but that only changes after you press enter which is useless to me. Someone else mentioned there is such an event, but couldn’t name it directly and I haven’t been able to find what they were talking about.

If anyone has any information on if such an event exists or is possible I’d love to know.

The Excel version is 2007 as far as I’m aware.

How to&Answers:

This, in my opinion, requires a non-programming solution. I absolutely sympathize – it is tough to watch people get old – but you have to draw the line somewhere – for their sake and yours. The enter key is the most basic part of a computer. You could probably write a macro that would automatically hit enter on every even(or odd depending) keystroke in excel – but you’re going to run into other problems like not being able to use delete normally. And what if you do want to put a string of text in a cell(like the student’s name)? Perhaps it is time to find a non-programming solution. By that I mean someone should have a candid conversation with him about how he wants to solve the problem. Personally, I would offer to type the numbers in for him, as I am accustomed to the number pad – but it is probably better to be more direct and start to discuss retirement.

See this discussion about the limitations of cell edit mode in excel:
http://www.mrexcel.com/forum/excel-questions/524860-call-macro-every-keystroke.html

If you’re really heart-set on a programming solution, I would recommend some kind of keystroke logging add-in.

Good Luck.

Answer:

You could use the Worksheet_SelectionChange event. It is triggered without enter, but it would be triggered a lot.

You could however also create a special user-form for typing in the data, but this might be more work than necessary.

The main problem with using my suggested event is, you will need it as trigger and trigger it yourself, when selecting the next row, so disable event handling before changing the selection.


Edit:

This is a quick solution (paste this into the vba-code of the desired worksheet):

Private Const clngColumnRightToLastGrade = 5

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Column = clngColumnRightToLastGrade Then
    Application.EnableEvents = False
    'offset selection, one row down, two cols to left
    Target.Offset(1, -2).Select
    Application.EnableEvents = True
  End If
End Sub

This will set you one row down and to column C, everytime your selection changes to column E (=5).

You don’t have to use a constant of course, you could specify the column to sense in the workbook, so your user might modify it easier by himself.

To make this as an optional feature, you could extend it to autogenerated code. What I have in mind is like a Ribbon-Button, which opens a setupForm to configure, and a Ribbon-Button to activate the configuration, which would place this code in the configured sheet. But this might be a bit over the top.

Answer:

In Excel 2003, (may be different in Excel2007 ?!) the WorkSheet_Change event is triggered every time the value of a cell is changed wether it is by pressing enter, delete, selecting an other cell after modifying a cell or even when a vba script changes the value of a cell.

I would do something like that:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RefRange As Range
    Set RefRange = Intersect(ActiveSheet.Columns("??????????"), ActiveSheet.UsedRange)
    If Not Intersect(Target, RefRange) Is Nothing Then
        Target.Offset(0, 1).EntireColumn.Range("A1").Select
        'Target.Offset(0, 1).EntireColumn.Range("A65536").End(xlUp).Offset(1,0).Select
    End If
End Sub