Dmitry Vasilyev
Dmitry Vasilyev

Reputation: 85

the hangman in VBA for Excel

Noticed some implementation of the hangman in other languages but not in VB for Excel, so decided to post a thread asking for your help...

The story in brief: ask user for the letter, if it corresponds to the one in the cells than make it black, if not make black one of the lines which at the end could result into the hangman shape.

Ok, after the edit of the code, it now produces 'Object Variable or With block variable not set' error; if I try to omit this part of line:

And sh.Line.ForeColor.RGB = RGB(255, 255, 255) 

Second issue is that the IF statements work in a wrong way, proceeding to ELSE and executing its content, despite the fact that colors of the text / colors of the hangman are correct and first IF should be triggered..

Option Explicit

Sub the_hangman()

Dim sh As Shape
Dim letter As String

Workbooks("Hangman.xlsm").Worksheets("Game").Activate

'Make all letters of the guess word white
 ActiveSheet.Range("B1", Range("B1").End(xlToRight)).Select
 Selection.Font.Color = vbWhite

 For Each sh In ActiveSheet.Shapes

   'Make all lines of the hangman shape white
   sh.Line.ForeColor.RGB = RGB(255, 255, 255)

Next sh

ActiveSheet.Range("B1").Activate

Do

'If the word wasn't guessed, and the hangman is still white then execute     this loop
'ERROR APPEARS HERE
 If ActiveSheet.Range("J1").Font.ColorIndex = RGB(255,255,255) And sh.Line.ForeColor.RGB <> RGB(255,255,255) Then

 letter = Application.InputBox("Please input a guess letter...", "The  Hangman")

        'If the letter guessed is same as in the corresponding cell then move to another _
        cell, inform user that he's right, color letter in black, and start over
        If letter = ActiveCell.Value Then
            ActiveCell.Font.ColorIndex = RGB(0, 0, 0)
            ActiveCell.Offset(0, 1).Activate
            MsgBox "The letter is correct! Keep going and you will win the game!"

        'If guessed letter is incorrect then make first line of the hangman shape black _
        by looping until the white line found, and inform the user that he's incorrect
        ElseIf letter <> ActiveCell.Value Then
            Do Until sh.Line.ForeColor.RGB = RGB(255, 255, 255)
                If sh.Line.ForeColor.RGB <> RGB(0, 0, 0) Then
                    sh.Line.ForeColor.RGB = RGB(0, 0, 0)
                End If
            Loop
            MsgBox "Incorrect guess, please try again!"
        End If

'If all thee letters are black - inform that user has won
ElseIf ActiveSheet.Range("J1").Font.ColorIndex = RGB(0, 0, 0) Then
MsgBox "You won! Congrats!"

'If the hangman shape is all black, then say that he lost
Else
   MsgBox "You lost!"
End If

Loop Until MsgBox("You won! Congrats!") Or MsgBox("You lost!")

End Sub

Upvotes: 3

Views: 1936

Answers (1)

paul bica
paul bica

Reputation: 10715

Remove the ".Value" after .Range("J1")

From:

ActiveSheet.Range("J1").Value.Font.ColorIndex = RGB(255,255,255)

To:

ActiveSheet.Range("J1").Font.ColorIndex = RGB(255,255,255)

Upvotes: 2

Related Questions