gpt4 book ai didi

vba - VBA for Excel 中的刽子手

转载 作者:行者123 更新时间:2023-12-02 02:03:52 25 4
gpt4 key购买 nike

注意到绞刑吏在其他语言中的一些实现,但在 VB for Excel 中没有,因此决定发布一个帖子寻求您的帮助...

故事简介:询问用户该字母,如果它与单元格中的字母相对应,则将其设为黑色,如果不将其中一条线设为黑色,最后可能会导致刽子手形状。

好的,编辑代码后,现在会产生“对象变量或未设置 block 变量”错误;如果我尝试省略这部分行:

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

第二个问题是 IF 语句以错误的方式工作,继续执行 ELSE 并执行其内容,尽管文本的颜色/刽子手的颜色是正确的并且应该触发第一个 IF。

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

最佳答案

删除.Range("J1")后面的“.Value

来自:

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

致:

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

关于vba - VBA for Excel 中的刽子手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com