Back Button within Userforms

Qmustard

New Member
Joined
Sep 27, 2016
Messages
37
Hi all,

I'm a bit stuck with regards to having a back button on a few of my userforms.

So scenario:

Userform 1 has Yes and No
Yes takes me to Userform2
No takes me to Userform3

Each of these Userforms have a Yes and No button on them as well as a Back button.

If I get to Userform4, I can get there from either Userform2 or Userform3.

The trouble is, I cant get the back button to work correctly with deciding how it goes back.

Ideally, if you got to Userform4 via 3, you'd go back to 3.
If you got there from 2, then you'd go back to 2.

But using the whatsclicked method doesnt work as it causes the Userforms themselves to get stuck in an ever ending loop of loading.

Sorry if this isnt clear enough!

I'm basically after a Back Button that can dynamically take you back to the previous userform you selected.
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows
Just asking. Why do you need two UserForms?

Why not do all you want on just one Form?

I could provide a way to just show you the controls you want when you want.
 

Qmustard

New Member
Joined
Sep 27, 2016
Messages
37
Eventually, I intend to have it all within one userform, but currently due to time I've got it so that I have 10 userforms, all with Yes and No buttons that guide someone to an outcome depending on what they've selected.
For the most part, this works perfectly, until I hit one of the userforms where you can reach it from more than one other userform, meaning the back button doesnt work to go back as it causes it to get stuck into an endless loop.

I'm not even sure If what I'm asking is possible.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows
Do you try and have all these forms opened at the same time?
Some how you would need a textbox that would have the Userform name entered when you load the form.
And then when you go back have the script go back to that form. When you say Go back to. Do you mean show.


I would create one Userform with 10 Mutipages.

Just click on what page you want to see. Each page would have a "Yes" "No" Button
 
Last edited:

Qmustard

New Member
Joined
Sep 27, 2016
Messages
37

ADVERTISEMENT

Well, basically it goes something like this:

As You click through the Userforms themselves, they hide away.

So if you select Yes as your first answer I have the code as:

Clicked Yes
Userform1.hide
Userform2.show vbmodeless

The problem I'm encountering is when it comes to userform5, you can reach that one from two other userforms.
So using a back button becomes problematic as I either settle on one back button outcome or try to code it so it tries to see what was previously selected and go back from that.

And yea, I guess I'm wording it wrong as the all the back button is really doing, is hiding the userform thats currently showing and looking to show the previous one. But I cant get it to select the previous one if theres two userforms that show that one userform.
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,387
Office Version
  1. 2019
Platform
  1. Windows
Hi,
As already stated by another here, your approach is probably not the most ideal approach but working with what you have, there may be a solution to achieving what you want.

following solution is not fully tested & so not too sure if what I have done is correct so no guarantees that it will work in way required but may give you some ideas to play with.

Make a BACKUP of your workbook & then place ALL the following code in a STANDARD module:

Rich (BB code):
 Public Sub ShowForm(ByVal FormName As String, ByVal LastForm As Object, ByVal Direction As XlSearchDirection)    
    Dim i As Integer
    On Error GoTo myerror
    LastForm.Hide
    For i = 0 To 9
    If UserForms.Item(i).Name = FormName Then
    With UserForms.Item(i)
        .Tag = IIf(Direction = xlNext, LastForm.Name, .Tag)
        .Show
    End With
    Exit For
    End If
    Next
myerror:
 If err > 0 Then MsgBox (Error(err)), 48, "Error"
End Sub


Sub LoadForms()
    Dim i As Integer
    Dim form As Object
    For i = 2 To 10
        Set form = UserForms.Add("UserForm" & i)
        Set form = Nothing
    Next i
End Sub


Sub UnLoadForms()
    Dim form As Object
    For Each form In UserForms
        Unload form
    Next form
End Sub

Place following code in the FIRST userform you open (USERFORM1 ?) code page

Rich (BB code):
Private Sub ButtonNo_Click()    
  ShowForm ("UserForm3"), Me, xlNext
End Sub


Private Sub ButtonYes_Click()
    ShowForm ("UserForm2"), Me, xlNext
End Sub


Private Sub UserForm_Initialize()
    LoadForms
End Sub

In subsequent userforms code pages

Rich (BB code):
Private Sub BackButton_Click()    
   ShowForm (Me.Tag), Me, xlPrevious
End Sub


Private Sub ButtonNo_Click()
    ShowForm ("UserForm5"), Me, xlNext
End Sub


Private Sub ButtonYes_Click()
    ShowForm ("UserForm4"), Me, xlNext
End Sub

You will need to amend the names of the userform each button opens shown in RED as required.

Solution should if it works, load all userforms in to memory when userform1 is shown - this allows you index through them based on which buttons you have pressed on each form.

You will at end of your process, need to unload all userforms from memory & this is done by calling the following code

Rich (BB code):
 UnLoadForms

You will need to insert in your project where appropriate.

Finally, I have assumed that you have named your usersforms “UserForm1” thru to “UserForm10”


Hope Helpful

Dave
 
Last edited:

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

I'm a little bit curious what these 10 Userforms do.

You mentioned they each have a "Yes" and "No" button that takes you back and forth from one UserForm to another but never said what is the purpose of the Userform.

What other controls are on the Userform and what do they do?
 

Qmustard

New Member
Joined
Sep 27, 2016
Messages
37
dmt32, I'll give that a go, thanks!

I'm a little bit curious what these 10 Userforms do.

You mentioned they each have a "Yes" and "No" button that takes you back and forth from one UserForm to another but never said what is the purpose of the Userform.

What other controls are on the Userform and what do they do?

Basically, I'm creating somewhat of a question matrix that users can use to come to an answer on what to do.

My questions are all Yes and No questions, with each answer taking you to the next userform, by hiding the previous one and showing the next.

My issue occurs when 2 of these userforms lead to one userform, how can I allow a back button to go back the right path without it failing.

So if you answer Userform1 or 5 with Yes, depending on the path you've gone down with answering the questions, they can both lead to Userform 10.
My issue is, with another command button, how can I get it to go abck to userform1 if thats where they answered from or from userform5 if thats the one they used.

If theres any other way I can do this whilst still using the seperate userforms for the time being im all ears, as I know I'm going to have to correct it or get it working somehow.

Ideally something like this:

Code:
If userform1.commandbutton1 = "Yes" Then
userform10.hide
userform1.show
ElseIf userform5.commandbutton1 = "Yes" Then
userform10.hide
userform5.show
End If

Is what I'm trying to achieve here, so the correct previous userform is shown when they click the back button.

For the most part, my back buttons are fine, as each question only leads to the next, but unfortunately some allow you to get to the same question meaning I have this issue.
 
Last edited:

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,387
Office Version
  1. 2019
Platform
  1. Windows
dmt32, I'll give that a go, thanks!

You may want to consider changing this line:

Code:
For i = 0 To 9

to this:

Code:
For i = 0 To UserForms.Count - 1


It's no big deal - I just hard coded number of userforms whilst testing - updated line makes solution dynamic & will adjust for any changes you may make to number of forms in your process.

Hope Helps

Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,123,259
Messages
5,600,575
Members
414,390
Latest member
plimbu

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
Top