VBA User Form passing

KHP

New Member
Joined
Apr 8, 2020
Messages
3
Office Version
  1. 2013
Hi,

'In "Private Sub UserForm_Activate()" I have a line as follows
'.......
nameControl1 = "Label_DisplayInformationForCurrentActivity"
nameControl2 = "Frame_Start"
Call M0_MacrosToF0.ResizingControls
'........

' In the Macro in module "M0_MacrosToF0" there is a line as follows"
'....
Select Case F0_StartForm.Controls(nameControl1).Width - F0_StartForm.Controls(nameControl2).Width
Case Is <= 0 'VARIABLE control is shorter than FIXED
With F0_StartForm.Controls(nameControl2)
F0_StartForm.Width = .Width + 30
.Left = (F0_StartForm.Width - .Width) / 2 - 5
End With
'....
'I am passing "nameControl1" and "nameControl2" to the sub.
'The question is:
'How do I pass the UserForm name the similar way that I am passing the "nameControl1" and "nameControl2", say, with nameForm1 = "F0_StartForm" in order the expression:
"F0_StartForm.Controls(nameControl1)" to look like "nameForm1.Controls(nameControl1)"?
'Many tries finish with error message.
'Thank you....
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi KHP and Welcome to the Board! It seems like you're interested in re-sizing userform controls. Here's some code that may help. Dave
 
Upvote 0
Dear Nd,
Thanks for your reply. Good examples for resizing.
My problem is not with that much with resizing.
My problem is in my question, which is as follows:
'How do I pass the UserForm name the similar way that I am passing the "nameControl1" and "nameControl2", say, with nameForm1 = "F0_StartForm" in order the expression:
"F0_StartForm.Controls(nameControl1)" to look like "nameForm1.Controls(nameControl1)"?
I am using many UserForms with different codes and I want to pass repeatedly to 1 single macro, which to be in a module.
At present I have as many macros as the UserForms are.
Can you help me on that?
 
Upvote 0
Sorry I don't understand. Pass the userform to the single macro in single module to do what? U just want to identify what userform name is? It should just be... eg. Userform1.ListBox1.ListIndex Maybe trial again to explain your needs. Dave
 
Upvote 0
OK.
When I pass from loaded UserForm code, which name is "F0_StartForm" to the macro code in the Module the variables like
......F0_StartForm.Controls(nameControl1)....., where the nameControl1 = "Label_DisplayInformationForCurrentActivity", it works OK.
The executable line then is read by the code as: "F0_StartForm.Controls(Label_DisplayInformationForCurrentActivity).BackColor = ...." (BackColor or Caption for example)...
But if I try to pass also the active UserForm name as a string variable, say, nameForm1 = "F0_StartForm" and so, to make the line in macro in module, like
......nameForm1.Controls(nameControl1)...., this does not work.
Could you please help me how this can be done?
 
Upvote 0
Hi,
you can pass your Userform as an Object to your procedure

example

Rich (BB code):
Private Sub UserForm_Activate()

    nameControl1 = "Label_DisplayInformationForCurrentActivity"
    nameControl2 = "Frame_Start"
    
    Call M0_MacrosToF0(Me, nameControl1, nameControl2)


End Sub

Note I have used the Me keyword as the argument for the userform


VBA Code:
Sub M0_MacrosToF0(ByVal Form As Object, ByVal ControlName1 As String, ByVal ControlName2 As String)
'....
    Select Case Form.Controls(ControlName1).Width - Form.Controls(ControlName2).Width
    Case Is <= 0 'VARIABLE control is shorter than FIXED
    With Form.Controls(ControlName2)
     Form.Width = .Width + 30
    .Left = (Form.Width - .Width) / 2 - 5
    End With

    End Select
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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