Change userform control property value indirectly.

HighAndWilder

Well-known Member
Joined
Nov 4, 2006
Messages
808
Office Version
  1. 365
Platform
  1. Windows
I'd like to be able to change the value of a userform control property by using a variable to hold the property name.

It seems that I can used the .Properties method for the userform but not for a control on the userform.

I get an error on the highlighted line.

Object doesn't support this property or method (Error 438)

This is just to demo the method.

Is there a way to do this or is it not possible?

Thanks

VBA Code:
Private Sub subChangePropertyValue(strControl As String, strProperty As String, varValue As Variant)
Dim ctrl As Control

  Set ctrl = Me.Controls(strControl)
  
 [B] ctrl.Properties(strProperty).Value = varValue[/B]
  
End Sub

Private Sub cmdButton_Click()
  Call subChangePropertyValue("cmdButton", "Top", Me.cmdButton.Top + 10)
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You want CallByName Method as follows:
VBA Code:
Private Sub subChangePropertyValue(strControl As String, strProperty As String, varValue As Variant)
    Dim ctrl As Control
    Set ctrl = Me.Controls(strControl)
    'ctrl.Properties(strProperty).Value = varValue
    CallByName ctrl, strProperty, VbLet, varValue
End Sub

Private Sub cmdButton_Click()
  Call subChangePropertyValue("cmdButton", "Top", Me.cmdButton.Top + 10)
End Sub

You would probably need to further refine the routine to also make it work for vbSet, vbMethod and vbGet
 
Upvote 0

Forum statistics

Threads
1,226,114
Messages
6,189,052
Members
453,522
Latest member
Seeker2025

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top