Passing a UserForm to a Sub as a Variable

C_Rieker

New Member
Joined
Nov 22, 2020
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello my favourite experts.

I am attempting to run a sub-routine every time a UserForm is initialised. The sub is used to format and position the UserForm elements based on values within a spreadsheet table. The code I use runs exceptionally well if I run it within the UserForm itself. However, my problem is that the code is quite repetitive, and I would rather not have to copy it into every UserForm coding (especially if I have to make a minor change to the code to all the UserForms.

To avoid the repetitive copy-paste, I have put the code into a Sub-Routine, and I pass the UserForm to the Sub as a variable. Only it stops and tells me Runtime Error: 13 - Type-Mismatch.

Any help would be greatly appreciated. Please see below my coding:

My UserForm Code:
VBA Code:
Private Sub UserForm_Initialize()

    InitiateUserForm (UF_NewProduct)

End Sub

My Sub-Routine:
VBA Code:
Sub InitiateUserForm(UFName As UserForm)

    Dim Table As Object
    Dim ItemName As String
    Dim cntrl As Control
    Dim UFString As String
    
    UFString = Right(UFName.Name, Len(UFName.Name) - 3)
    
    Set Table = Sheets("UF LAYOUT").ListObjects("UFLayout_" & UFString)
    
    With UFName
        On Error Resume Next
            ItemName = .Name
            .Caption = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Caption").DataBodyRange).Value
            .Top = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Top").DataBodyRange).Value
            .Left = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Left").DataBodyRange).Value
            .Height = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Height").DataBodyRange).Value
            .Width = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Width").DataBodyRange).Value
        On Error GoTo 0
    End With
    
    For Each cntrl In UFName.Controls
        On Error Resume Next
            ItemName = cntrl.Name
            cntrl.Caption = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Caption").DataBodyRange).Value
            cntrl.Top = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Top").DataBodyRange).Value
            cntrl.Left = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Left").DataBodyRange).Value
            cntrl.Height = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Height").DataBodyRange).Value
            cntrl.Width = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Width").DataBodyRange).Value
            cntrl.Visible = WorksheetFunction.XLookup(ItemName, Table.ListColumns("Name").DataBodyRange, Table.ListColumns("Visible").DataBodyRange).Value
        On Error GoTo 0
    Next cntrl

End Sub

The Error:
Error13.PNG
 
Dave I could kiss you!!! That fixed my problem. Bless you.

Just one question...Why did the brackets make a difference?

looks like Norrie kindly provided answer to your question

You would enclose argument in parentheses or brackets if using the Call statement but it's use is not a requirement

VBA Code:
Call InitiateUserForm(Me)

Glad issue resolve for you & appreciate the feedback

Dave
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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