Want to become an expert in VBA? So this is the right place for you. This blog mainly focus on teaching how to apply Visual Basic for Microsoft Excel. So improve the functionality of your excel workbooks with the aid of this blog. Also ask any questions you have regarding MS Excel and applying VBA. We are happy to assist you.

Reference Dynamic Userform Name


If you want to reference a one particular  userform you can do it in a very simple way.  What you can do is you can just use the name of the  userform.  For an example,  think you have a userform call UserForm1. If you want to show  this   userform  you can simply use following code.

Sub ShowForm()

UserForm1.Show

End Sub


But sometimes we need to reference userforms dynamically.  For an example assume you have  several userforms.  You need to show a different forms depending on different conditions.  And there is a text box where program generate the name of the userform you need to display. That means you need to show the userform which is shown in this text box. Then we can't use above simple method.  Because we need to reference userforms  dynamically. So we need a more dynamic solution like below. Here txtDisplayFormName refers to name of the textbox where program displays the name of the userform  we need to show.

Dim formName As String

formName = txtDisplayFormName.Value

Dim DForm As Object

Set DForm = UserForms.Add(formName)

DForm.Show

Contact Form

Name

Email *

Message *

Popular Posts