How to forward a UserForm.Name as a parameter to a Sub

SeniorNewbie

Board Regular
Joined
Jul 9, 2023
Messages
77
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
  2. MacOS
Hi out there!
I'm playing around with userforms and controls. With this:
VBA Code:
Sub GetUserForms()
Dim usf As Object, sUSF As String

    For Each usf In ThisWorkbook.VBProject.VBComponents
        If usf.Type = 3 And Left(usf.Name, 3) <> "the" Then
            sUSF = usf.Name
            MsgBox sUSF, , "1: "
            test sUSF
        End If
    Next

End Sub

Sub test(sUSF As String)
MsgBox sUSF, , "2: "
Dim oUSF As Object
    Set oUSF = sUSF
    MsgBox oUSF.Caption, , "2:: "

End Sub
I get all existing userfroms of a workbook. My next step ist to write some details of the included controls to a sheet (this part works). Now I want to handover the usf.name in a loop to the next sub. I tried objects, msForms.Userform, strings and variants.

How can I do this?

THX a lot
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Do you compile your code before trying to run it? That should tell you that there is a problem - you can't assign a string to an object:
Set oUSF = sUSF

I suspect that you should pass the control to the sub if you want to work with its properties. AFAIK, a sub cannot return a value to your calling code, so if you want to pass anything with the result of the called procedure back to the calling procedure you would use a function.
 
Upvote 0
Not sure what you're attempting to do but I played with your altered code to demo passing a control and using its properties. However, given that you will pass any "object" rather than specific types, you'll run into issues where the object type does not have the property you're testing for (e.g. caption).

VBA Code:
Sub GetUserForms()
Dim usf As Object

For Each usf In ThisWorkbook.VBProject.VBComponents
    If usf.Type = 3 Then test usf
Next

End Sub

Sub test(usf As Object)
    MsgBox usf.Caption
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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