gpt4 book ai didi

excel - 在 VBA 中执行用户窗体控件时,对象不支持此属性错误

转载 作者:行者123 更新时间:2023-12-02 11:33:16 24 4
gpt4 key购买 nike

我有一个 VBA 代码,用于查找用户窗体控件的类型并向 Excel 工作表中的某些单元格添加注释。这些 Useform 控件是动态的。我的意思是,像文本框、标签等控件是通过使用另一个宏插入的。效果很好。我使用类似这样的格式来添加这些控件::set Label1i = UserForm2.Controls.Add("Forms.Label.1", "Test"& labelCounter, True)。当我从同一用户窗体上的命令按钮调用以下子程序时。我收到“运行时 438 错误:对象不支持此属性或方法”

以下代码已成功运行。但是,当我向该程序添加一个手动“文本框”时,它显示了此错误。

Private Sub CommandButton1_Click()
Dim cCont As Control
Dim Commnts As String
Commnts = ""
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" And cCont <> "" Then
Commnts = cCont
Sheet1.Range(cCont.ControlTipText).AddComment Commnts
End If

Next cCont

If Commnts <> "" Then
Unload UserForm2
MsgBox ("Comments updated")
Call Graphic16_Click
Else
MsgBox ("Nothing to update")
End If
End Sub

有人可以帮我解决这个问题吗?

最佳答案

cCont <> ""

I am getting "Runtime 438 error: Object doesn't support this property or method"

@SiddharthRout: Yes. There is an image already in controls. Yes..yes.. after introducing that, I go this issue. – user3342652 7 mins ago

如果您的用户窗体上有一个没有默认属性的控件,您将收到该错误。例如Image控制。该控件没有像Textbox/Range/CommanButton这样的默认属性。等都有一个。

你可以不用说

Debug.Print Textbox1
Debug.Print Range("A1")
Debug.Print Commandbutton1

但是下面会报错

Debug.Print Image1 '<~~ This will give error 

所以当你说cCont <> ""时,您正在尝试比较 String使用控件的默认属性(在本例中没有),因此您会收到错误 Runtime 438 error: Object doesn't support this property or method

尝试处理该错误,看看它所说的精确控制是什么?

 Private Sub CommandButton1_Click()
Dim cCont As Control

On Error GoTo Whoa

For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" And cCont <> "" Then

End If
Next cCont

LetsContinue:

Exit Sub
Whoa:
MsgBox Err.Description & vbNewLine & _
"Control Name : " & cCont.Name & _
vbNewLine & _
"Control Type : " & TypeName(cCont)
Resume LetsContinue
End Sub

例如

enter image description here

解决方案:

要解决此问题,请打破 IF分为 2 个部分,如下所示。第二个If仅当满足第一个条件时才会检查。

If TypeName(cCont) = "TextBox" Then
If cCont <> "" Then

End If
End If

关于excel - 在 VBA 中执行用户窗体控件时,对象不支持此属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352053/

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