gpt4 book ai didi

vba - 从特定模块更改复选框的字体属性 "Strikethrough"不起作用

转载 作者:行者123 更新时间:2023-12-04 11:59:49 24 4
gpt4 key购买 nike

使用 VBA(Excel 365(16.0.12527.20880),德语)我试图将复选框( Checkbox1)的删除线属性设置为 False按照Office VBA reference这应该有效。
以下代码放在Module1中(简化)并更改 UserForm1.Checkbox1 的删除线值( UserForm1Checkbox1 是静态的,通过 VBA 编辑器创建,而不是在运行时通过代码创建)。

    Sub ChangeCheckBox()

UserForm1.CheckBox1.Caption = "Test" 'this triggers the Init-Procedure, which only sets the Height and Width of the UserForm. This shouldn't effect the problem, but I'm mentioning it here so it's clear that the form has been initialized. But if I leave it out, it's the same problem.

'Pre-Test - works fine

'Check initial value
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns false (as it should)

'Write value and check it
UserForm1.CheckBox1.Font.Strikethrough = False
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns false (as it should)

'Write value and check it again
UserForm1.CheckBox1.Font.Strikethrough = False
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns false (as it should)

'This next line seeems to cause the post-test failure

tmpString = ThisWorkbook.Worksheets("Sheet1").Cells(tmpIndex, tmpColumn).Value
Debug.Print tmpString 'returns the correct value

'Post-Test - fails!!!

'Check initial value
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns false (as it should)

'Write value and check it
UserForm1.CheckBox1.Font.Strikethrough = False
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns true (should still be false)

'Write value and check it again
UserForm1.CheckBox1.Font.Strikethrough = False
Debug.Print UserForm1.CheckBox1.Font.Strikethrough 'returns true (definitely should return false now)

End Sub
就我而言,出于某种原因,该命令将框设置为 True而不是 False .到目前为止,我已将问题定位到特定的代码行。 “预测试”成功,“后测试”失败(其他一切正常)。请注意,我正在逐步使用 Debug模式,没有其他代码在“中间”执行。
如果我创建一个不同的 Form 并在那里尝试同样的事情,问题仍然存在。原来的程序有几百行代码,但是我创建了上面的Testprocedure,问题依旧。我还可以通过从头开始创建一个新的 Excel 文件来重现它,只需一张工作表、一个用户表单/复选框和一个包含测试程序的模块。
那么,为什么 tmpString = ThisWorkbook.Worksheets("Sheetname").Cells(tmpIndex, tmpColumn).Value导致“后测试”失败?
注:这个错误不能在所有机器上重现,我在不同的机器上试过。在那里,我无法重现该错误。

最佳答案

我无法重现这个问题,我已经在 3 台不同的机器上尝试过。
但是,我记得大约 7 年前看到过类似的问题,我设法通过首先检查是否需要更改字体属性的值来解决它。
尝试替换所有出现的这个:

UserForm1.CheckBox1.Font.Strikethrough = False
有了这个:
If UserForm1.CheckBox1.Font.Strikethrough Then UserForm1.CheckBox1.Font.Strikethrough = False
这基本上设置了 .Font.Strikethrough属性(property)到 False仅当它已经是 True因为否则就是 False (显然)并且如果该属性的 setter 中有任何错误,那么这将跳过该错误。

与您的问题无关,但使用用户表单的默认实例并不是一个好主意,就像您在这里对 Userform1.Checkbox... 所做的那样。 .我建议你阅读 this article由@MathieuGuindon 于 2017 年撰写。

关于vba - 从特定模块更改复选框的字体属性 "Strikethrough"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68916663/

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