Where does the code go?

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, This is probably one of those really obvious kind of questions but I have scoured the internet and this forum and I can't find the answer; so I am probably using the wrong search terms...

I have built a ton of userforms but I have never used the multipage form before so I thought I would convert some of my existing forms into a single one. Many of you gurus have recommended that to others and it sure makes sense to do that. Building it was straight forward enough, but when it comes to placing my code on there I only get a single "place" (if I could say it that way) to place all my code. Apparently I do not place the code for each page on its own page?

I see the
Code:
Private Sub MultiPage1_Change()

End Sub
presented when I add my code, so I am guessing I need to write syntax to designate which set of subs go to which page. Would this just be "MultiPage1", or "MultiPage2" and so on?

Also, since each of my current userforms contains a myriad of subs for all of the controls for each one. I am guessing that I cannot drop my existing codes directly into this sub since then I would be having a bunch of subs in a sub... Could someone show me an example of the code that I would use to get this code into my new userform?

I appreciate any assistance - Rick
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Not exactly sure what your question is but:

Mutipages work just like basic UserForm. Put Command button and all type controls on them and they work just like UserForm

If you want to refer to Mutipage controls you refer to them like this.

Me.MultiPage1.Page("Bill").Textbox1.value="Cake"

"Bill" be the name of the Multipage on Multipage1

Or You could have Me.Multipage2.Page("Sam").Textbox1.value="Pie"

Me refers to active UserForm.

Or Me.Multipage3.Page("John").Textbox2.value=Sheets(1).Range("A1").value

Not sure if this helps.
 
Upvote 0
Thanks for responding My Answer. I guess my confusion is when I right click and select "view code" on the regular userform, I get that sheet and I pop my code into that form, and I am all set. However, if i try to do it on the userform that contains a multipage, I cannot right click on each of the separate pages (or tabs as it were) and put my different set of subs for each page or tab. I guess that they all go in the main userform page - which totally makes sense to me.

So, for example if I had a userform with 4 pages, and each page had identical sets of controls on it and such. And I wanted the following set of subs to be run from the each page (this is a really reduced set of controls, I just copied over a couple of them; and some pages may actually be different). How would I format the code inside of the "MultiPage1_Change() sub. I am pretty sure that I cant just pop it into that sub straight away because I would have it ending...end sub end sub.... and I think I'll get a compile error. And if I just putting the same code on the same sheet only separated by the multipage 1 and the multipage2 I will get an ambiguous name error.

Is the answer to repeat the mutlipage"#"_change() event for each page and then just make sure that all of the controls and items have different names? So in this case I would repeat all of the subs 4 times, each one in its own Multipage_change sub, and rename all of the controls to make them unique?

(I was thinking of just trying it but I want to ask your advice before I go further because we are talking a LOT of items to rename, so I don't want to do it and then find out I was still doing it wrong - lol)

Code:
Private Sub MultiPage1_Change()


End Sub


Code:
Private Sub ClearButton_Click()
TextBox1.Value = ""
TextBox1.SetFocus
End Sub


Private Sub InspectionAreaButton_Click()
    Unload EmergencyLightingInspections
    FacilitiesInspections.Show
End Sub




Private Sub UserForm_Initialize()
    Dim i As Long
    Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "E").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, 5).Value = "" Then ListBox1.AddItem Cells(i, 2).Value
    Next
    Me.TextBox4.Text = CStr(ThisWorkbook.Sheets("Emergency Lighting Log").Range("C1").Value)
    TextBox1.SetFocus


End Sub
 
Upvote 0
Like I said earlie Multipage controls are no different from any other.

Put a button on your Multipage
Double click the button when in vba edit mode and put in your code.

The code needs to look like this:

Multipage1.Pages(0).Textbox1.value="John"

Pages(0) is first page on your Multipage Page(1) would be second page on your Multipage
 
Upvote 0
As far as moving all your other code into this Userform and use Multipages I'm not sure how to do all that and explain it all to you.
 
Upvote 0
oh, duh - all I have to do is add the "multipage1.page(0)" in front of all of my controls; that makes total sense. Thanks for saving me again and sorry for you to have to explain it to me twice. :)
 
Upvote 0
I had thought you could say Multipage1.Pages("George") if page was named "George" but could not get that to work.

It's Multipage1.Pages(0)

It it's your second page it would be:

Multipage1.Pages(1)
We all learn more here every day.

oh, duh - all I have to do is add the "multipage1.page(0)" in front of all of my controls; that makes total sense. Thanks for saving me again and sorry for you to have to explain it to me twice. :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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