Adding buttons to userforms

andyh69uk

New Member
Joined
Nov 23, 2005
Messages
31
I am trying to write some code that will add a command button to a userform, I am trying to achieve the following:-

Userform1 containing 1 button to close userform1
Userform2 containing 1 button to close userform2 and open userform1
Userform3 containing 1 button to close userform 3 and open useform1, but also to add another button to useform1 which will re-open userform3

Think that makes sense, I have tried various ways and got it working by adding code to the activate event off userform1 based on a varibale set by the button on userform3. This worked but I couldn't define any action for the button, if I added the code in the intialise event of userform1 the button did not get added. I tried repaint and put msgbox's in the code to check it was executing and everying seemed fine apart from the lack of any button.

Any ideas??

Cheers

Andy
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Do you want code to use the buttons as you indicated or do you want code to actually build UserForms and Buttons by code?
 

andyh69uk

New Member
Joined
Nov 23, 2005
Messages
31
I want the button on userform3 to trigger code in userform1 that adds a button at runtime.
 

andyh69uk

New Member
Joined
Nov 23, 2005
Messages
31
I have just had a thought and think that maybe it would be better to have the button exist in the first place and just set its visibility to false. Then make it appear if a varibale has been set. Is there anyway in VBA to have different constructors for userforms, by that I mean different ways to call a form. ie if userform2 calls userform1 do this.......however if userform3 calls userform1 do this..............I suppose I mean can you have different intialise events???
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,061
Office Version
  1. 365
Platform
  1. Windows
Andy

Have you tried using the Visible property of the buttons you are adding to the userform(s)?

By the way what are you actually trying to achieve?
 

andyh69uk

New Member
Joined
Nov 23, 2005
Messages
31
Sorry wasn't thinking very straight last night, obviously I can create different constructors I just need to write them as procedures to call the userform. I have used the visible setting on the button to achieve the result I required.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,826
Messages
5,574,530
Members
412,601
Latest member
TheBeaniacExpress
Top