4

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.

4
  • Error pointing on section :: If TypeName(cCont) = "TextBox" And cCont <> "" Then Commented Aug 5, 2019 at 4:04
  • Not able to Reproduce the error, it's working fine Commented Aug 5, 2019 at 4:37
  • Are you sure you are adding only a textbox and not anyother comtrol like say an "Image1" control? Commented Aug 5, 2019 at 5:14
  • @SiddharthRout: Yes. There is an image already in controls. Yes..yes.. after introducing that, I go this issue. Commented Aug 5, 2019 at 5:19

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.