Display several userform one after another

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Hi.I have more than 30 forms on my workbook that I want to display one by one automatically. How can I do that? I tried the following code but it does not work well

VBA Code:
UserForm1.Show
        UserForm1.Repaint
        Application.Wait Now + TimeValue("00:00:10")
        UserForm1.Hide
        
        UserForm2.Show
        UserForm2.Repaint
        Application.Wait Now + TimeValue("00:00:10")
        UserForm2.Hide
        
        UserForm3.Show
        UserForm3.Repaint
        Application.Wait Now + TimeValue("00:00:10")
        UserForm3.Hide
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I have more than 30 forms on my workbook that I want to display one by one automatically
Do you need to open, and then immediately hide?

If so, try the following:

In a module:
VBA Code:
Sub Repaint_Forms()
  Dim i As Long
  Dim uForm As Object
 
  For i = 1 To 3      '<<<---- Change to 30
    Set uForm = CallByName(UserForms, "Add", VbMethod, "UserForm" & i)
    With uForm
      .bck = True
      .Show
    End With
  Next
End Sub

When you open a Userform, the Userform has control of the process, so it is in the Userform where you must indicate: Repaint and Hide, then in each Userform you must put the following:
VBA Code:
Option Explicit

Public bck As Boolean

Private Sub UserForm_Activate()
  If bck Then
    Application.Wait Now + TimeValue("00:00:02")
    Me.Repaint
    Me.Hide
  End If
End Sub

The bck variable, I am using it so that the Userform knows that it should be repainted and hidden.
Because I suppose that if you open the Userform individually it should not be hidden.

Do the test with 3 userforms.

Another detail that you should not miss, when you Show a userform and hide it, the userform is in memory, if you do the process several times without downloading the userform from memory, you may have a memory saturation error, that is why it is important download the userforms from memory, for that use the following macro:

VBA Code:
Sub Unload_Forms()
  Dim i As Long
  Dim uForm As Object
 
  For i = 1 To 3
    Set uForm = CallByName(UserForms, "Add", VbMethod, "UserForm" & i)
    Unload uForm
  Next
End Sub


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Do you need to open, and then immediately hide?

If so, try the following:

In a module:
VBA Code:
Sub Repaint_Forms()
  Dim i As Long
  Dim uForm As Object
 
  For i = 1 To 3      '<<<---- Change to 30
    Set uForm = CallByName(UserForms, "Add", VbMethod, "UserForm" & i)
    With uForm
      .bck = True
      .Show
    End With
  Next
End Sub

When you open a Userform, the Userform has control of the process, so it is in the Userform where you must indicate: Repaint and Hide, then in each Userform you must put the following:
VBA Code:
Option Explicit

Public bck As Boolean

Private Sub UserForm_Activate()
  If bck Then
    Application.Wait Now + TimeValue("00:00:02")
    Me.Repaint
    Me.Hide
  End If
End Sub

The bck variable, I am using it so that the Userform knows that it should be repainted and hidden.
Because I suppose that if you open the Userform individually it should not be hidden.

Do the test with 3 userforms.

Another detail that you should not miss, when you Show a userform and hide it, the userform is in memory, if you do the process several times without downloading the userform from memory, you may have a memory saturation error, that is why it is important download the userforms from memory, for that use the following macro:

VBA Code:
Sub Unload_Forms()
  Dim i As Long
  Dim uForm As Object
 
  For i = 1 To 3
    Set uForm = CallByName(UserForms, "Add", VbMethod, "UserForm" & i)
    Unload uForm
  Next
End Sub


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Thank you. Unfortunately, I was not able to put the code correctly. I found this code to work well. The problem is that I cannot specify the time period for each model to appear and hide individually. I have to specify a specific duration for each of them, for example user1 30 seconds, user2 15 seconds and so on.....



VBA Code:
Option Explicit

Sub View_User()
Dim Model, A, J

Model = Array(Login_screen, UserForm1, UserForm2, UserForm3, UserForm4, UserForm5, UserForm6, UserForm7, UserForm8, UserForm9, UserForm10 _
      , UserForm11, UserForm12)
      
Application.Visible = False
For A = 0 To UBound(Model)

    J = Timer + 2 'sec
    
    Model(A).Show 0
    While Timer < J And J < 86400: DoEvents: Wend
    Unload Model(A)
Next
Application.Visible = True
End Sub
 
Upvote 0
And if you explain to us what the objective you are looking for is, that is, why do you want to show a userform if you are going to close it, what is the final objective?
 
Upvote 0
The idea is that I create an educational program for children so that I show each model a picture with a certain caption for a certain duration. Each image has a specific timing. I also aspire in the future to the possibility of adding sound if I can. Maybe it's a bit of a stupid idea because I might have reached 50 models over time.
 
Upvote 0
Try the following.

On sheet "Sheet1" put the names of the forms in column A and in column B the time for each form, as shown in the following example:

Dante Amor
AB
1Userform NameTime Seconds
2Login_screen30
3UserForm140
4UserForm215
5
Sheet1



Run this macro:
VBA Code:
Sub View_User()
  Dim uForm As Object
  Dim i As Long
  Dim arr_forms As Variant
  Dim nameform As String
 
  arr_forms = Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
  Application.visible = False
  For i = 1 To UBound(arr_forms)
    nameform = arr_forms(i, 1)
    Set uForm = CallByName(UserForms, "Add", VbMethod, nameform)
    DoEvents
    uForm.Show 0
    Application.Wait Now + TimeValue("00:00:" & arr_forms(i, 2))
    DoEvents
    Unload uForm
  Next
  Application.visible = True
End Sub

🫡


-------------
I also aspire in the future to the possibility of adding sound

Other idea.
What you need sounds like a presentation
You could do it in Power Point. There you can add a time for each slide and music too.
:giggle:
 
Upvote 0
Solution
Try the following.

On sheet "Sheet1" put the names of the forms in column A and in column B the time for each form, as shown in the following example:

Dante Amor
AB
1Userform NameTime Seconds
2Login_screen30
3UserForm140
4UserForm215
5
Sheet1



Run this macro:
VBA Code:
Sub View_User()
  Dim uForm As Object
  Dim i As Long
  Dim arr_forms As Variant
  Dim nameform As String
 
  arr_forms = Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
  Application.visible = False
  For i = 1 To UBound(arr_forms)
    nameform = arr_forms(i, 1)
    Set uForm = CallByName(UserForms, "Add", VbMethod, nameform)
    DoEvents
    uForm.Show 0
    Application.Wait Now + TimeValue("00:00:" & arr_forms(i, 2))
    DoEvents
    Unload uForm
  Next
  Application.visible = True
End Sub

🫡


-------------


Other idea.
What you need sounds like a presentation
You could do it in Power Point. There you can add a time for each slide and music too.
:giggle:
Very cool idea. Thank you
 
Upvote 0
Try the following.

On sheet "Sheet1" put the names of the forms in column A and in column B the time for each form, as shown in the following example:

Dante Amor
AB
1Userform NameTime Seconds
2Login_screen30
3UserForm140
4UserForm215
5
Sheet1



Run this macro:
VBA Code:
Sub View_User()
  Dim uForm As Object
  Dim i As Long
  Dim arr_forms As Variant
  Dim nameform As String
 
  arr_forms = Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
  Application.visible = False
  For i = 1 To UBound(arr_forms)
    nameform = arr_forms(i, 1)
    Set uForm = CallByName(UserForms, "Add", VbMethod, nameform)
    DoEvents
    uForm.Show 0
    Application.Wait Now + TimeValue("00:00:" & arr_forms(i, 2))
    DoEvents
    Unload uForm
  Next
  Application.visible = True
End Sub

🫡


-------------


Other idea.
What you need sounds like a presentation
You could do it in Power Point. There you can add a time for each slide and music too.
:giggle:
you are creative. After experience, this is what is actually required. Just a question, please, can I add audio clips to each model used in the same way? Because the idea is as previously mentioned. The file is a file of audio and video instruction lessons. With your effort, the image was obtained. I'm missing the sound now. Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
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