Odd behaviour - running program without clicking on code

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,207
I have recently come across a program whereby if I merely hover the mouse over a particular line of code, the program runs.

Has anyone come across such odd behaviour?
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
12,864
Office Version
365, 2010
Platform
Windows, Mobile
You can do that with jQuery in a browser if that is what you mean.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,207
You can do that with jQuery in a browser if that is what you mean.
No I don't think I mean jQuery.


This program was taken from a training website.

It consists of three classes and a standard module.

ClsGame:

Code:
Option Explicit

Const MaxGuesses As Integer = 10

Public StopGameStatus As enumStopGameStatus
Private HangmanBook As Workbook

Private pWordToGuess As String
Property Get WordToGuess() As String

WordToGuess = pWordToGuess

End Property

Property Let WordToGuess(ThisGuessWord As String)
   
pWordToGuess = ThisGuessWord

Dim WordCount As Integer
WordCount = Len(pWordToGuess)


Range(Cells(1, 1), Cells(1, WordCount)).Name = "Word"
Range(Cells(1, WordCount + 1), Range("A1").End(xlToRight)).EntireColumn.Hidden = True
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).EntireRow.Hidden = True

With Range("Word")

.EntireRow.RowHeight = 40

.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter

.Font.Bold = True
.Interior.Color = RGB(240, 240, 240)

End With

Dim c As Range

For Each c In Range("Word").Cells

With c
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).LineStyle = xlContinuous

.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).LineStyle = xlContinuous

.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous

.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous

.EntireColumn.ColumnWidth = 15

End With

Next c

Range("B3").Value = "Correct"
Range("B4").Value = "Wrong"
Range("B5").Value = "Left"

Range("A3").Name = "Correct"
Range("A4").Name = "Wrong"
Range("A5").Name = "Left"

Range("C3").Name = "GuessesCorrect"
Range("C4").Name = "GuessesWrong"
Range("C5").Name = "GuessesLeft"

Dim SpacedAlphabet As String
Dim LetterPosition As Integer

SpacedAlphabet = ""
For LetterPosition = 1 To Len(Alphabet)
SpacedAlphabet = SpacedAlphabet & Mid(Alphabet, LetterPosition, 1) & " "
Next LetterPosition
Range("GuessesLeft").Value = SpacedAlphabet

Range("Correct").Value = 0
Range("Wrong").Value = 0
Range("Left").Value = MaxGuesses

End Property

Private Sub Class_Initialize()
 
Set HangmanBook = Workbooks.Add

StopGameStatus = GameInProgress

Dim ws As Worksheet

For Each ws In Worksheets
If ws.Name <> ActiveSheet.Name Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next ws

ActiveSheet.Name = "Hangman"

End Sub

Private Sub Class_Terminate()
  
    HangmanBook.Close savechanges:=False

End Sub

Private Sub RemoveLetter(WhichLetter As String)
  
    Range("GuessesLeft").Value = Replace(Range("GuessesLeft").Value, " " & WhichLetter, "")

End Sub

Sub PlayRound()

Dim Letter As New clsGuess

Letter.CorrectWord = pWordToGuess

If Not Letter.IfTooManyGoes Then

StopGameStatus = UserKeptGuessingInvalidLetters

Exit Sub

End If

If Letter.IfAlreadyGuessed Then

MsgBox "You've already guessed this letter!"

Exit Sub

End If

RemoveLetter Letter.LetterGuessed

If Letter.IfGuessCorrect Then

Range("GuessesCorrect").Value = Range("GuessesCorrect").Value & " " & Letter.LetterGuessed
Range("Correct").Value = Range("Correct").Value + 1

If IfWordGuessed Then
StopGameStatus = UserWon
Exit Sub
End If

MsgBox "Good guess! Letter " & Letter.LetterGuessed & " was in the word.", vbOKOnly + vbExclamation, "Correct guess"

Else

Range("GuessesWrong").Value = Range("GuessesWrong").Value & " " & Letter.LetterGuessed
Range("Wrong").Value = Range("Wrong").Value + 1
Range("Left") = Range("Left").Value - 1

If Range("Left").Value = 0 Then

StopGameStatus = UserLost

Exit Sub

End If

MsgBox "Sorry: letter " & Letter.LetterGuessed & " is not in the word.", vbOKOnly + vbExclamation, "Wrong guess"

End If

End Sub

Private Function IfWordGuessed() As Boolean
   
Dim c As Range

For Each c In Range("Word")

If c.Value = "" Then
IfWordGuessed = False
Exit Function
End If

Next c

IfWordGuessed = True

End Function
ClsGuess:

Code:
Option Explicit

Public LetterGuessed As String
Public CorrectWord As String

Private Sub Class_Initialize()

LetterGuessed = ""

End Sub

Public Property Get IfTooManyGoes() As Boolean
  
If LetterGuessed = "" Then StartGuess

IfTooManyGoes = (LetterGuessed <> "")

End Property

Private Sub StartGuess()

Dim Letter As String

Const MaxGuesses As Integer = 3

Dim GuessNumber As Integer

GuessNumber = 1
LetterGuessed = ""

Do Until Len(LetterGuessed) > 0 Or GuessNumber > MaxGuesses

Letter = UCase(InputBox("Think of a letter", _
"Guess", "Type letter here"))

If Len(Letter) <> 1 Then
MsgBox "You must type in one (and only one) letter"
ElseIf InStr(1, Alphabet, Letter) <= 0 Then
MsgBox "Not a valid letter"
Else

LetterGuessed = Letter

End If

GuessNumber = GuessNumber + 1

Loop

End Sub

Property Get IfAlreadyGuessed() As Boolean

    Dim i As Integer
Dim GuessesSoFar As String

GuessesSoFar = Range("GuessesCorrect").Value & Range("GuessesWrong").Value
GuessesSoFar = Replace(GuessesSoFar, " ", "")

For i = 1 To Len(GuessesSoFar)

If Mid(GuessesSoFar, i, 1) = LetterGuessed Then
IfAlreadyGuessed = True
Exit Property
End If

Next i

IfAlreadyGuessed = False

End Property

Property Get IfGuessCorrect() As Boolean

IfGuessCorrect = False

Dim i As Integer
Dim c As Range

For i = 1 To Len(CorrectWord)

If Mid(CorrectWord, i, 1) = LetterGuessed Then

Set c = Cells(1, i)
c.Interior.Color = RGB(240, 255, 255)
c.Value = LetterGuessed
IfGuessCorrect = True

End If

Next i

End Property
ClsWord:

Code:
Option Explicit
Public WordChosen As String
Private Sub Class_Initialize()
  
Dim PossibleWords(9) As String

PossibleWords(0) = "one"
PossibleWords(1) = "two"
PossibleWords(2) = "three"
PossibleWords(3) = "four"
PossibleWords(4) = "five"
PossibleWords(5) = "six"
PossibleWords(6) = "seven"
PossibleWords(7) = "eight"
PossibleWords(8) = "nine"
PossibleWords(9) = "ten"


Dim wordNumber As Integer
wordNumber = Int(Math.Rnd() * 10)
WordChosen = PossibleWords(wordNumber)

End Sub
Standard module:

Code:
Option Explicit
Public Const Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Public Enum enumStopGameStatus

GameInProgress = 0
UserLost = 1
UserWon = 2
UserKeptGuessingInvalidLetters = 3

End Enum

Sub PlayHangman()

    Dim IfPlayAgain As Boolean

If MsgBox("Debugging?", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then Stop


IfPlayAgain = True

Do Until Not IfPlayAgain

IfPlayAgain = False

Dim game As New clsGame

Dim w As New clsWord

game.WordToGuess = UCase(w.WordChosen)
Set w = Nothing

Do Until game.StopGameStatus <> GameInProgress

game.PlayRound

Loop

Select Case game.StopGameStatus

Case enumStopGameStatus.UserWon

MsgBox "Congratulations - you've won! The word was " & game.WordToGuess & "."

Case enumStopGameStatus.UserLost

MsgBox "Sorry - you lost. The word was " & game.WordToGuess & "."

Case enumStopGameStatus.UserKeptGuessingInvalidLetters

MsgBox "Aborted game"

Case Else

MsgBox "Error in code"

End Select

If MsgBox("Do you want to play again?", vbQuestion + vbYesNo + vbDefaultButton2, "Play again?") = vbYes Then IfPlayAgain = True

Set game = Nothing

Loop

End Sub
Try these steps.

1. Open the Hangman game and enable macros.

2. Go to the VB editor, look for the class module, clsGame and Sub PlayRound and put break point on this line:

Code:
StopGameStatus = UserKeptGuessingInvalidLetters
3. Press the Play button.

4. When the Debugging messagebox pops up, click No.

5. When the Guess message box pops up, click Cancel.

6. When the "You must type in one (and only one) letter" message box pops up, click OK

7. Click Cancel

8. Click OK

9. Click Cancel

10. Click OK


Now the program should hit the break point.

Look for this line (found in the class module, clsGame and Sub PlayRound):

Code:
If Not Letter.IfTooManyGoes Then
Hovering the mouse over the word Not causes the program continues to run and the Guess message box pops up.

Does it happen to you as well?
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,670
Office Version
2013
Platform
Windows
That sounds strange. I'd restart your computer and hope it stops...
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,207
That sounds strange. I'd restart your computer and hope it stops...
Unfortunately it happens every time.

Try it for yourself if you're interested.
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,670
Office Version
2013
Platform
Windows
Its odd yes. Maybe the mouseover invokes code in the property get that creates additional execution in the program.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,207
Its odd yes. Maybe the mouseover invokes code in the property get that creates additional execution in the program.
So you experienced it too?
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,697
Office Version
2010
Platform
Windows
Happens to me too, but not on "Not". Instead, it's "Letter" that triggers the continuation of the code.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,114
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
How would you expect it to show you the value of the property without executing the code?
 

Watch MrExcel Video

Forum statistics

Threads
1,102,110
Messages
5,484,812
Members
407,467
Latest member
spurs50

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top