The Change event doesn't know that you've just typed a space.
You probably want to look at the BeforeUpdate event and process everything
at the end
or the KeyUp event, where you check after each word.
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = Asc(" ") Then
Beep
End If
End Sub
An approach might be to reprocess the whole sentence every word. Just to
accommodate copy-paste actions / inserting words between words etc..
--
Rob van Gelder -
http://www.vangelder.co.nz/excel
"jasonsweeney " wrote in
message ...
Rob,
I am trying to modify your code so that it executes in the
textbox1_change event. Let me explain.
I am trying to get it so that if the user enters a word in textbox1
that is not in the dictionary, then a message comes up "word not in
dictionary" and then the word is parsed into individual letters and
placed back into the textbox as the person types.
I am not having much success so far. There are several problems to
overcome. The first is that I don't know how to add text into a
textbox that already contains sub-strings....if I set textbox.value =
str(1), then all the previous text is overwritten by the new value. I
have tried using the clipboard as a temporary repository for the
existing text, then paste it back and add-on the sub-strings, but I
have not been successful with that either.
Second, the str = Split(TextBox1.Text, " ") command acts different when
in the textbox1_change event. The code seems to trigger whenever any
two sub-strings are joined together (e.g. try typing "the" and the code
does not find "th" in the dictionary.) It needs to check for the
string using the " " delimiter. Thus, the code should not check to see
if the word is in the dictionary until the space bar is pressed...to
add a level of complexity here, " " is necessarily in the dictonary
itself as a "word". (e.g. the space character is assigned its own 5
digit code number.)
To reproduce what I have, I typed in separate cells in a vertical
column: "the", "quick", "brown", "fox", "a", "b", "c", ... "z", "".
(*** Note the space character). I then named this range "dictionary")
I have userform3 code as follows:
_________________________________________________
Private Sub CommandButton1_Click()
Dim str() As String
Dim i As Long, k As Long
str = Split(TextBox1.Text, " ")
k = 1
For i = LBound(str) To UBound(str)
Sheet1.Cells(k, 1).Value = str(i)
k = k + 1
Next
UserForm3.Hide
End Sub
Private Sub TextBox1_Change()
Dim str() As String, strTemp As String
Dim lngIndex As Long, i As Long, j As Long, k As Long
str = Split(TextBox1.Text, " ")
k = 1
On Error Resume Next
For i = LBound(str) To UBound(str)
lngIndex = Application.VLookup(str(i), Range("Dictionary"), 2, False)
If Err.Number Then
Err.Clear
UserForm3.TextBox1.Copy
MsgBox "Word not in dictionary: '" & str(i) & "' ;Parse Required."
For j = 1 To Len(str(i))
strTemp = Mid(str(i), j, 1)
lngIndex = Application.VLookup(strTemp, Range("Dictionary"), 2, False)
If Err.Number Then
Err.Clear
MsgBox "letter not in dictionary: " & strTemp
Else
'
' requirements:
' needs to (1) erase the word not in dictionary from the textbox
'leaving the rest of the text in place,
' (2) parse the word into individual letters with a space between
' each letter and a space at the end, (3) needs to insert this
' new string in the textbox in the place where the word-not-in
' -dictonary existed, and
' (4) allows the user to continue typing his or her paragraph
'
UserForm3.TextBox1.Value = strTemp
k = k + 1
End If
Next
End If
Next
End Sub
---
Message posted from http://www.ExcelForum.com/