Refer to a UserForm using variables

trl123

New Member
Joined
Sep 28, 2017
Messages
6
I'm trying to create a piece of formatting code that is applicable to multiple UserForms when they initialise respectively. This is the code I have tried so far where FormatUserFormName is the dynamic string name of the UserForm that I am trying to apply the formatting code to.

I currently get Run-time error '438'. Object doesn't support this property or method.

Code:
Public FormatFurnaceLocation As String
Public FormatFurnaceNumber As String
Public FormatUserFormName As String
Public Sub HideFurLocDetails()

On Error Resume Next

Dim BTNFurLocPullPart As Object
Dim LBLFurLocHeatNumber As Object
Dim LBLFurLocHeatNumberLBL As Object
Dim LBLFurLocTimer As Object
Dim LBLFurLocTimerLBL As Object
Dim FormatUserForm As Object
Dim Location As Integer

Set FormatUserForm = ThisWorkbook.VBProject.VBComponents(FormatUserFormName)

For Location = 65 To 88

  Set BTNFurLocPullPart = FormatUserForm.Controls("BTNFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "PullPart")
  Set LBLFurLocHeatNumber = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "HeatNumber")
  Set LBLFurLocHeatNumberLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "HeatNumberLBL")
  Set LBLFurLocTimer = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "Timer")
  Set LBLFurLocTimerLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "TimerLBL")

  BTNFurLocPullPart.Visible = False
  LBLFurLocHeatNumberLBL.Visible = False
  LBLFurLocHeatNumber.Visible = False
  LBLFurLocTimerLBL.Visible = False
  LBLFurLocTimer.Visible = False

Next Location
End Sub

Thanks all for your help in advance!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,
try these changes

Rich (BB code):
Public FormatFurnaceLocation As String
Public FormatFurnaceNumber As String
Public FormatUserFormName As String



Public Sub HideFurLocDetails(ByVal FormatUserForm As Object)


On Error Resume Next


Dim BTNFurLocPullPart As Object
Dim LBLFurLocHeatNumber As Object
Dim LBLFurLocHeatNumberLBL As Object
Dim LBLFurLocTimer As Object
Dim LBLFurLocTimerLBL As Object
Dim FormatUserForm As Object
Dim Location As Integer


Set FormatUserForm = ThisWorkbook.VBProject.VBComponents(FormatUserFormName)


For Location = 65 To 88


  Set BTNFurLocPullPart = FormatUserForm.Controls("BTNFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "PullPart")
  Set LBLFurLocHeatNumber = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "HeatNumber")
  Set LBLFurLocHeatNumberLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "HeatNumberLBL")
  Set LBLFurLocTimer = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "Timer")
  Set LBLFurLocTimerLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "TimerLBL")


  BTNFurLocPullPart.Visible = False
  LBLFurLocHeatNumberLBL.Visible = False
  LBLFurLocHeatNumber.Visible = False
  LBLFurLocTimerLBL.Visible = False
  LBLFurLocTimer.Visible = False


Next Location
End Sub

Delecte ALL code lines shown in RED



When calling the code from your userform pass an instance of the Form using the ME keyword as an argument.

Rich (BB code):
Rich (BB code):
HideFurLocDetails Me


It is assumed that your code is in a standard module.

Dave
 
Upvote 0
Hi Dave,

Thanks a lot for your response.

When I try to initialise the UserForm, I get a Run-time error "Could not find the specified object".

I've double-checked all of the controls that I've stated the variable names of and they're correct. Do I need to define FormatUserForm in the respective UserForm that I'm calling the code from?

Thanks again!
 
Upvote 0
I suspect the problem is with this code construct:

Rich (BB code):
Set BTNFurLocPullPart = FormatUserForm.Controls("BTNFur" & FormatFurnaceNumber & "Loc" & FormatFurnaceLocation & "PullPart")

If items shown in RED are controls on your form then you will need to qualify them

Rich (BB code):
With FormatUserForm
  Set BTNFurLocPullPart = .Controls("BTNFur" & .FormatFurnaceNumber & "Loc" & .FormatFurnaceLocation & "PullPart")
  Set LBLFurLocHeatNumber = .Controls("LBLFur" & .FormatFurnaceNumber & "Loc" & .FormatFurnaceLocation & "HeatNumber")
  Set LBLFurLocHeatNumberLBL = .Controls("LBLFur" & .FormatFurnaceNumber & "Loc" & .FormatFurnaceLocation & "HeatNumberLBL")
  Set LBLFurLocTimer = .Controls("LBLFur" & .FormatFurnaceNumber & "Loc" & .FormatFurnaceLocation & "Timer")
  Set LBLFurLocTimerLBL = .Controls("LBLFur" & .FormatFurnaceNumber & "Loc" & .FormatFurnaceLocation & "TimerLBL")
End With

and I assume in your Controls string construct that Set Statement is referring to a valid control object?

Dave
 
Upvote 0
Thank you for your quick response.

I had forgotten to place Chr(Location) so that it pulled the letters.

Sorry for wasting your time with the second question, you had solved it first time!

The correct code in the module is:

Code:
Public FormatFurnaceNumber As String
Public Sub HideFurLocDetails(ByVal FormatUserForm As Object)
On Error Resume Next
Dim BTNFurLocPullPart As Object
Dim LBLFurLocHeatNumber As Object
Dim LBLFurLocHeatNumberLBL As Object
Dim LBLFurLocTimer As Object
Dim LBLFurLocTimerLBL As Object
Dim Location As Integer
For Location = 65 To 88
Set BTNFurLocPullPart = FormatUserForm.Controls("BTNFur" & FormatFurnaceNumber & "Loc" & Chr(Location) & "PullPart")
Set LBLFurLocHeatNumber = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & Chr(Location) & "HeatNumber")
Set LBLFurLocHeatNumberLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & Chr(Location) & "HeatNumberLBL")
Set LBLFurLocTimer = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & Chr(Location) & "Timer")
Set LBLFurLocTimerLBL = FormatUserForm.Controls("LBLFur" & FormatFurnaceNumber & "Loc" & Chr(Location) & "TimerLBL")
BTNFurLocPullPart.Visible = False
LBLFurLocHeatNumberLBL.Visible = False
LBLFurLocHeatNumber.Visible = False
LBLFurLocTimerLBL.Visible = False
LBLFurLocTimer.Visible = False
Next Location
End Sub

And the following in the respective UserForm:
Code:
    ModUF2_03_FormattingSubs.FormatFurnaceNumber = "150"
    HideFurLocDetails Me
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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