Forum

Getting control whe...
 
Notifications
Clear all

Getting control when a character is entered in a cell

3 Posts
2 Users
0 Reactions
221 Views
(@nels)
Posts: 8
Active Member
Topic starter
 

How do I get control in my main macro code when a character is entered in a spreadsheet cell? It appears that a character is only entered in the cell after the keystroke occurs and a different cell is selected by the mouse.  Then I detect it by monitoring for the cell’s change in a loop in the main macro code.

 
Posted : 01/04/2022 6:03 pm
(@debaser)
Posts: 836
Member Moderator
 

The worksheet_change event is triggered by changing a cell's contents manually. Given all your similar posts, it would probably make sense for you to give more of an overview of what you are doing and why you appear to want a main piece of code to be running constantly.

 
Posted : 02/04/2022 4:23 am
(@nels)
Posts: 8
Active Member
Topic starter
 

Ms, Miss, or Mrs Velouria:

I'm attempting to make a "Wordle" like game (named Fourdle) that is played in an Excel worksheet.

It currently only uses 4 letter words.  My intent is to make a tool used by elementary level school teachers which allows them to create 4 letter words and their definitions.  Where the definitions are partially revealed as the student gets more of the letters in his guess correct.

The capability that I would like is to gain control each time the user enters a keystroke in a cell or selects a shape.  I understand I can use the OnAction operator on shapes.  I don’t know if such ability exists related to keystrokes.  Furthermore, since OnAction requires calling a subroutine, I have had to resort to the artifice of:

  1. On Fourdle macro startup, deleting a signal file if it exists.
  2. In the OnAction subroutine, writing the signal file to disk.
  3. Constantly monitoring for the file’s existence in an infinite loop in the Fourdle subroutine, and when the file is detected, performing the appropriate VBA operation.

This “write a file to disk” kludge could be greatly simplified if VBA had global variables (like the “C” language) or there was a mechanism to communicate back to the main (Fourdle) subroutine when a subordinate subroutine (invoked via an OnAction operation) is executed.

Currently keystroke input is detected by starting with space characters in all 4 cells in a game line and then constantly monitoring the cells for changed value until all 4 cells on a game line are entered, then evaluating the entered characters and advancing to the next game line.  This creates a significant CPU load which could be greatly alleviated by a OnAction like operator that gains control after a cell’s value changes.

I am in an infinite loop in the fourdle macro code

Sub fourdle()
Dim version As String 'Macro revision version number.
Dim number_of_worksheets As Integer 'Total number of sheets in this workbook.

...

'Create path to signal file in the current working directory.
signal_file_name = ThisWorkbook.Path & "" & Environ("username")

'If the signal file exists on game startup, then delete it.
If Len(Dir(signal_file_name)) > 0 Then
    Kill signal_file_name
End If

...

'Create the game stop button.
'Set stop_execution = Worksheets(game_sheet_pos).Shapes.AddShape(msoShapeRectangle, 20, 20, 72, 72)
sh_left = Cells(2, 10).Left
sh_top = Cells(2, 10).Top
sh_width = Cells(2, 10).Width
sh_height = Cells(2, 10).Height
Set stop_execution = ActiveSheet.Shapes.AddShape(msoShapeRectangle, sh_left, sh_top, sh_width, sh_height)
stop_execution.Fill.ForeColor.RGB = colorx(cyanx)
stop_execution.OnAction = "StopAndSave"
stop_execution.TextEffect.Text = "STOP & SAVE"

'The game setup and display has been established now so start the infinite loop which monitors for word guess input.

Check_For_New_Input:
While (1)

...

'The player enters characters in cells while this loop is executing

‘When all 4 cells on the current game line are entered, compare the

‘cells to the current game word and evaluate each cell according to whether

‘the word is correctly identified, etc.  The game session advances to a new word when either the player correctly specifies the word’s spelling, or when all the guesses are exhausted

...

Waiting_For_All_Four:
start_time = Timer
DoEvents
Application.Wait (Now + TimeValue("00:00:03"))
end_time = Timer
'MsgBox ("Event delay = " & (finish - Start))
wait_time = end_time - start_time

If Len(Dir(signal_file_name)) > 0 Then
GoTo StopAndSave1
End If

x = x 'Breakpoint place holder.
Wend

 

StopAndSave1:
'MsgBox ("GAME TERMINATED BY USER")

'Change the stop box to red to indicate the game is stopping.
stop_execution.Fill.ForeColor.RGB = colorx(redx)
DoEvents

'The logic to write the current game history to a disk file with a name unique to the player has yet to be written, but goes here.
x = x 'Breakpoint
GoTo Macro_Exit

'This subroutine gets called when the player presses the "Stop & Save" button.

Sub StopAndSave()
Dim signal_file_name As String
Dim output_file_number As Long

signal_file_name = ThisWorkbook.Path & "" & "Signal"

If Len(Dir(signal_file_name)) <= 0 Then
'Signal file does not exist.

'Get the next available file number.
output_file_number = FreeFile() 'Open the raw output file.
Open signal_file_name For Output As output_file_number Len = 8192
Write #output_file_number, "Signal"
Close #output_file_number
Else
'Ignore multiple requests to quit.
End If
x = x 'Breakpoint
End Sub

 
Posted : 09/04/2022 6:21 pm
Share: