4
votes

I have a VBA code for finding type of the Userform controls and add comments to some of the cells in my excel sheet. These Useform controls are dynamic. I meant, This controls like Textbox, label, etc are inserted by using another macro. It works fine. I use something like this format to add those controls:: set Label1i = UserForm2.Controls.Add("Forms.Label.1", "Test" & labelCounter, True). When I call below sub from the commandbutton on same userform. I am getting "Runtime 438 error: Object doesn't support this property or method"

Below code was running successfully. However, when I add one more manual "Textbox" to this program, it showed this error.

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

Could someone please help me on this.

1
Error pointing on section :: If TypeName(cCont) = "TextBox" And cCont <> "" Thenuser3342652
Not able to Reproduce the error, it's working fineMikku
Are you sure you are adding only a textbox and not anyother comtrol like say an "Image1" control?Siddharth Rout
@SiddharthRout: Yes. There is an image already in controls. Yes..yes.. after introducing that, I go this issue.user3342652

1 Answers

2
votes

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

You will get that error if you have a control on your userform which doesn't have a defualt property. For example an Image control. This control doesn't have a defualt propery like a Textbox/Range/CommanButton etc has one.

You can get away with saying

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

but the below will give an error

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

So when you say cCont <> "", you are trying to compare a String with the control's default property (which in this case there is none) and hence you are getting the error Runtime 438 error: Object doesn't support this property or method

Try and handle the error and see what exact control is it talking about?

 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

For example

enter image description here

Solution:

To handle this, break the IF in 2 parts as shown below. The second If will only be checked when the 1st condition is met.

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

    End If
End If