Search code examples
excelvbatextboxuserform

Excel 2016 Userform Textbox Properties


I'm having an issue with the Userform Textbox properties. I'm trying to write a simple sub to change the color of a textbox to gray when it is disabled. The code I'm attempting to write (which works on other VB Forms outside of Excel) is:

Private Sub ColorTxtBoxDisable(txtSel As TextBox)
    txtSel.BackColor = Color.Gray
End Sub

Where txtSel is an argument accepting a Textbox control name. However, declaring a variable/argument as Textbox brings up an entirely different set of properties available through the code, and Backcolor is not one of them. I've had no trouble changing textbox properties like this in other VB Forms, but Excel doesn't want to cooperate. Any thoughts would be appreciated.


Solution

  • txtSel is an argument accepting a TextBox control. Not its name. It's an object reference, where TextBox is a type defined in whatever first referenced type library that contains a TextBox type, since it's unqualified. Since Excel is the host application, it takes precedence over the MSForms type library, which defines the TextBox control you mean to be using. Thus, VBA resolves that TextBox to Excel.TextBox, and that's not the textbox you're looking for.

    enter image description here

    The VBA standard library and the host application object model library can't be moved down or un-referenced.

    Qualify the type with the library it's from. And pass it ByVal while you're at it, since there's no reason to (implicitly) pass it ByRef here.

    Private Sub ColorTxtBoxDisable(ByVal txtSel As MSForms.TextBox)