I have a Userform Control Panel that I am making for a workbook. I have a page named #1, which is for the workbook sheet #1. I also have an 'Add sheet' button that copies the #1 page and creates a #2 page.
The problem is that the code for the controls on the #1 page do not work on the newly created #2 page. And I don't know what the page #2 controls are called so I can't make code for it beforehand.
This is the Copy/Paste code that I found somewhere. Page 0 is the General settings page and page 1 is the #1 page. I have a Frame taking up the whole Multipage area so it copies the frame and everything in it and copies it.
Option Explicit
Private Sub AddProgramButton_Click()
Dim l As Double, r As Double
Dim ctl As Control
Dim PAGECOUNT As Long
MultiPage1.Pages.Add
MultiPage1.Pages(1).Controls.Copy
PAGECOUNT = MultiPage1.Pages.Count
MultiPage1.Pages("Page" & PAGECOUNT).Paste
MultiPage1.Pages("Page" & PAGECOUNT).Caption = "#" & PAGECOUNT - 1
For Each ctl In MultiPage1.Pages(1).Controls
If TypeOf ctl Is MSForms.Frame Then
l = ctl.Left
r = ctl.Top
Exit For
End If
Next
For Each ctl In MultiPage1.Pages(PAGECOUNT - 1).Controls
If TypeOf ctl Is MSForms.Frame Then
ctl.Left = l
ctl.Top = r
Exit For
End If
Next
End Sub
OK, I haven't got much information from you to go with, but I can make this work by using the following method. If you want to use it, you will have to modify it to suit your own needs.
To follow this example, you will need to create a new UserForm, preferably in a new workbook, and follow my instructions below.
I have created a UserForm as you state, with a Multipage - currently I have pages 0 and 1 on it. Page 0 I am ignoring for the purposes of this example (you mentioned it was just the General Settings page).
Seperate from the Multipage, I have put the main CommandButton (the one which actually adds the new Page when it's clicked) and have named it AddProgramButton as you did.
On Page 1, I have a frame as you state. Within this frame, I have put a CommandButton, a TextBox and a ComboBox on mine. I don't know what your controls are, but you will need to follow my example for now.
Now we need to start entering the code. First, if you haven't already got one, insert a Standard Module in your project. At the top of this standard module, enter the following code:
Option Explicit
Public myButtonArr() As New CButton
Public myComboArr() As New CCombo
Public myTextBoxArr() As New CTextBox
Now, in your UserForm module, you should input the following (note that some of this is the information you first provided):
Option Explicit
Private Sub UserForm_Initialize()
Dim ctl As Control
For Each ctl In MultiPage1.Pages(1).Controls
Select Case TypeName(ctl)
Case Is = "CommandButton"
ReDim Preserve myButtonArr(1 To 1)
Set myButtonArr(1).myButton = ctl
Case Is = "ComboBox"
ReDim Preserve myComboArr(1 To 1)
Set myComboArr(1).myCombo = ctl
ctl.AddItem "A"
ctl.AddItem "B"
Case Is = "TextBox"
ReDim Preserve myTextBoxArr(1 To 1)
Set myTextBoxArr(1).myTextBox = ctl
End Select
Next ctl
End Sub
Private Sub AddProgramButton_Click()
Dim l As Double, r As Double
Dim ctl As Control
Dim PAGECOUNT As Long
MultiPage1.Pages.Add
MultiPage1.Pages(1).Controls.Copy
PAGECOUNT = MultiPage1.Pages.Count
MultiPage1.Pages("Page" & PAGECOUNT).Paste
MultiPage1.Pages("Page" & PAGECOUNT).Caption = "#" & PAGECOUNT - 1
For Each ctl In MultiPage1.Pages(1).Controls
If TypeOf ctl Is MSForms.Frame Then
l = ctl.Left
r = ctl.Top
Exit For
End If
Next
For Each ctl In MultiPage1.Pages(PAGECOUNT - 1).Controls
If TypeOf ctl Is MSForms.Frame Then
ctl.Left = l
ctl.Top = r
Exit For
End If
Next
For Each ctl In MultiPage1.Pages(PAGECOUNT - 1).Controls
Select Case TypeName(ctl)
Case Is = "CommandButton"
ReDim Preserve myButtonArr(1 To PAGECOUNT - 1)
Set myButtonArr(PAGECOUNT - 1).myButton = ctl
Case Is = "ComboBox"
ReDim Preserve myComboArr(1 To PAGECOUNT - 1)
Set myComboArr(PAGECOUNT - 1).myCombo = ctl
ctl.AddItem "A"
ctl.AddItem "B"
Case Is = "TextBox"
ReDim Preserve myTextBoxArr(1 To PAGECOUNT - 1)
Set myTextBoxArr(PAGECOUNT - 1).myTextBox = ctl
End Select
Next ctl
End Sub
Now, for each control I have within the frame, we need to create a new Class. Insert three new Class Modules. You must name these as follows:
CButton
CCombo
CTextBox
Now open the CButton class module, and insert the following code:
Option Explicit
Public WithEvents myButton As MSForms.CommandButton
Private Sub myButton_Click()
MsgBox "You clicked the button on one of the pages"
End Sub
Next, open the CCombo class module, and insert the following code:
Option Explicit
Public WithEvents myCombo As MSForms.ComboBox
Private Sub myCombo_Change()
MsgBox "You changed the value of the ComboBox on one of the pages"
End Sub
Finally, open the CTextBox class module, and insert the following code:
Option Explicit
Public WithEvents myTextBox As MSForms.TextBox
Private Sub myTextBox_Change()
MsgBox "You changed some text in the TextBox on one of the pages"
End Sub
Now, if you test your Userform, it should work. You should hopefully be able to modify my example to match your own requirements.
Note: the events in the class module will produce an identical response regardless of which page is selected. You will have to modify the code yourself (or provide more information) to "personalise" the results.
BTW you probably found your original code here: Copy Elements From One Page To Another in Multipage with VBA in Excel.
Related
I wrote some code in a class module and userform_initialize_event. All are okay. But when I Dim the new class module and applies in the initialize event it says that variable not defined. Here is my code -
Dim Buttons() As New BtnClass
Private Sub UserForm_Initialize()
Dim ButtonCount As Integer
Dim ctl As Control
' Create the Button objects
ButtonCount = 0
For Each ctl In fmHover.Controls
If TypeName(ctl) = "Label" Then
ButtonCount = ButtonCount + 1
ReDim Preserve Buttons(1 To ButtonCount)
Set Buttons(ButtonCount).ButtonGroup = ctl
End If
Next ctl
End Sub
If I Dim the Buttons inside the event it does not throws any error and the code also don't works. I searched in a lot of place. Every one Dim the new class before the Initialize event. So, why my one doesn't work? Kindly suggest where is my mistake.
Here is my class module code -
Public WithEvents ButtonGroup As MSForms.Label
Private Sub ButtonGroup_Click()
Msg = "You clicked " & ButtonGroup.Name
MsgBox Msg
ButtonGroup.Name
End Sub
Thanks in advance.
Each label on your form should have it's own instance of the class. The form needs a way to remember each instance - stored in a collection at the form level.
Not sure what the ButtonCount is doing, so have removed it.
Your form code:
Option Explicit '!!!Add option explicit to the top of each module!!!
'It helps avoid so many errors that I don't think three
'exclamations marks is enough!!!!
'>Tools>Options>tick Require variable declaration
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'Each instance of the class is stored here.
Private colLabels As Collection
Private Sub UserForm_Initialize()
Dim BtnEvents As BtnClass
'Initialise the collection to hold the class instances.
Set colLabels = New Collection
Dim ctrl As Control
For Each ctrl In Me.fmHover.Controls
If TypeName(ctrl) = "Label" Then
Set BtnEvents = New BtnClass 'New instance of the class.
Set BtnEvents.AssignButton = ctrl 'Assign the label to it.
colLabels.Add BtnEvents 'Add it to the collection so it's remembered.
End If
Next ctrl
End Sub
Your class module will look like:
Option Explicit 'More exclamation marks!!!!!!!!!
Public WithEvents ButtonGroup As MSForms.Label
'Let the class know what control it's assigned to.
Public Property Set AssignButton(ctrl As Control)
Set ButtonGroup = ctrl
End Property
Private Sub ButtonGroup_Click()
Dim Msg As String
Msg = "You clicked " & ButtonGroup.Name
MsgBox Msg
End Sub
I need the help with the selection of multiple checkboxes by user. I have a userform in vba that has 8 checkboxes. I want user to select only ONE checkbox at a time. What is the shortest coding possible?
Please, try the next way:
Create a Sub able to untick all the check boxes, except the active one:
Public Sub UnTickCheckBoxes() 'Public to be also called from outside if use a class for event allocation
Dim i As Long
For i = 0 To Me.Controls.count - 1
If TypeName(Me.Controls(i)) = "CheckBox" And Not ActiveControl.Name = Me.Controls(i).Name Then
Application.EnableEvents = False
Me.Controls(i).value = False
Application.EnableEvents = True
End If
Next i
End Sub
Call the above sub from the Click event of all check boxes. You can use Change event for doing something else. The call can be done in this way:
Private Sub CheckBox1_Click()
If ActiveControl.value = True Then
UnTickCheckBoxes
End If
End Sub
2 bis. Automatic event allocation to all check boxes:
2.1 Create a class named "chkBoxesClickEvent". Copy and paste in it the next code:
Option Explicit
Public WithEvents chkBEvent As MSForms.CheckBox
Private Sub chkBEvent_Click()
If chkBEvent.value = True Then
chkBEvent.Parent.UnTickCheckBoxes
End If
End Sub
2.2 Declare on top of the form (in the declarations area) the next variable:
Private chkB() As New chkBoxesClickEvent
2.3 Place the next code in the UserForm_Initialize event:
Private Sub UserForm_Initialize()
Dim C As MSForms.Control, k As Long
ReDim chkB(Me.Controls.count): k = 1
For Each C In Me.Controls
If TypeOf C Is MSForms.CheckBox Then
Set chkB(k).chkBEvent = C: k = k + 1
End If
Next
ReDim Preserve chkB(k - 1)
End Sub
Do not forget to clear all check boxes Click event, if any code inside...
Show the form a see how it works, then send some feedback.
I have a multipage, I was successfully able to copy elements of the first page which is my reference page to new pages which is created dynamically.
My question is, how do I set a commandbutton's actions inside a page in a multipage control?
My goal is to click on the button from any page then pops up another form.
How do I do this?
It's pretty hard to adjust from Android to VB. I really appreciate any help from you guys.
This is my code in cloning pages.
i = 0
MultiPage1.Pages.Add
MultiPage1.Pages(i).Controls.Copy
i = i + 1
MultiPage1.Pages(i).Paste
For Each ctl In Me.MultiPage1.Pages(i).Controls
If TypeOf ctl Is MSForms.Label Then
'~~~ code omitted
Select Case ctl.Tag
Case "startTime"
ctl.Caption = "4:00pm"
End Select
End If
Next
this is how it's going to look like.
the button will concatenate all strings inside the page. the concatenated string will be shown on another userform.
You probably would be better off creating a button in the ribbon so that it is available on all pages:
http://chandoo.org/wp/2012/02/27/how-to-add-your-own-macros-to-excel-ribbon/
EDIT:
My bad, I thought you meant a worksheet instead of a VBA MultiPage in a userform.
Check this out. I was able to make this work for me:
Assign code to a button created dynamically
Class1:
Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton
Private Sub CmdEvents_Click()
MsgBox "yo"
End Sub
Userform with MultiPage object:
Option Explicit
Dim cmdArray() As New Class1
Private Sub CommandButton1_Click()
Dim newControl As Control
Set newControl = Me.MultiPage1.Pages(0).Controls.Add("Forms.CommandButton.1", "NewCommand", True)
newControl.Object.Caption = "hello"
newControl.Left = 50
newControl.Top = 50
ReDim Preserve cmdArray(1 To 1)
Set cmdArray(1).CmdEvents = newControl
Set newControl = Nothing
End Sub
You can do this with a custom class. The class basically has one member Public WithEvents b as CommandButton.
The trick is the WithEvents keyword. You can now insert some code to generically handle the click of a button that is assigned to this class:
Private Sub b_Click()
MsgBox "You clicked " & b.Name 'Modify this event so that different code is executed base on the page/name/etc.
End Sub
In order to make this work, you need to assign the button you create in your code to an object of this new class:
Private objButtonHandler as New MyClass 'this should be scope a UserForm wide
Sub YourSub
Dim objYourButton as CommandButton
Set objYourButton = ... `your code here
Set objButtonHandler.b = objYourButton
End Sub
I have a multipage in a userform. During run-time, the user can choose to add x number of pages at any time. The elements of each page will be the same. I am wondering if there is a way to duplicate these elements, or would I need to re-create these same elements for each new page? If so, how do I specify locations on the page where the element should be placed?
The trick is to put all controls in a frame in the 1st page and then the rest becomes easy :)
This code will copy the controls from Page1 to Page2 after creating Page2 and align them accordingly.
Option Explicit
Private Sub CommandButton2_Click()
Dim l As Double, r As Double
Dim ctl As Control
MultiPage1.Pages.Add
MultiPage1.Pages(0).Controls.Copy
MultiPage1.Pages(1).Paste
For Each ctl In MultiPage1.Pages(0).Controls
If TypeOf ctl Is MSForms.Frame Then
l = ctl.Left
r = ctl.Top
Exit For
End If
Next
For Each ctl In MultiPage1.Pages(1).Controls
If TypeOf ctl Is MSForms.Frame Then
ctl.Left = l
ctl.Top = r
Exit For
End If
Next
End Sub
SNAPSHOT
The "Run-time error '-2147417949 (80010108)' may be caused by having a Frame somewhere else on the form. Try removing any other frames and running again.
On a Userform, I'm blinking a frame Off/On by toggling its visiblity. It blinks a variable number of times and then stops, but in between blinks it checks for user activity. If there has been a mouse click anywhere on the form or on any of the contained controls then the blinking stops immediately.
This is what my blinker looks like.
For i = 1 To numberOfBlinks
<blink twice>
DoEvents
If <click detected> Then Exit Sub
Next i
Everything works fine except for the <click detected> part. How do I do that from inside the loop?
Did you tried to change a global boolean variable on the mouseclick event to true (default false)?
Then try to check if this global boolean variable is true in <click detected>.
This seems to work ok, but it looks like a lot of code just to detect a mouse click. For instance, I thought it should be possible to create a Class that contains all the Form Controls, so I could detect a click on any of them in one go, without having to check on each kind of control separately. I couldn't make that work and I'm hoping somebody can improve on this.
Just to restate what this does: On a Userform, a large frame named mapFrame holds any number of other frames and labels, and all those contained frames can hold any number of other frames and labels, but that's as deep as the nesting goes. I want to start a loop, (in this case the loop blinks a control off and on, but it could be any other loop) and wait for the user to click on any of the contained Frames or Labels to signal an exit from the loop. I also want to get the name of the control that was clicked.
I took the suggestion by therealmarv and used the click to set a public Boolean which gets tested inside the loop.
In a new Class Module:
Option Explicit
Public WithEvents classLabels As msForms.Label
Private Sub classLabels_Click()
clickedControlName = "" '<== Public String
With classLabels
If .Parent.Name = "mapFrame" Or _
.Parent.Parent.Name = "mapFrame" Then
isClickDetected = True '<== Public Boolean
clickedControlName = .Name
End If
End With
End Sub
In another new Class Module:
Option Explicit
Public WithEvents classFrames As msForms.Frame
Private Sub classFrames_Click()
clickedControlName = "" '<== Public String
With classFrames
If .Name = "mapFrame" Or _
.Parent.Name = "mapFrame" Or _
.Parent.Parent.Name = "mapFrame" Then
isClickDetected = True '<== Public Boolean
clickedControlName = .Name
End If
End With
End Sub
In a Form Module:
Option Explicit
Dim frames() As New clsFrames
Dim labels() As New clsLabels
Private Sub createFrameListeners()
Dim ctl As msForms.Control
Dim frameCount as Long
For Each ctl In Me.Controls
' Debug.Print TypeName(ctl): Stop
If TypeName(ctl) = "Frame" Then
frameCount = frameCount + 1
ReDim Preserve frames(1 To frameCount)
'Create the Frame Listener objects
Set frames(frameCount).classFrames = ctl
End If
Next ctl
End Sub
Private Sub createLabelListeners()
Dim ctl As msForms.Control
Dim LabelCount as Long
For Each ctl In Me.Controls
' Debug.Print TypeName(ctl): Stop
If TypeName(ctl) = "Label" Then
LabelCount = LabelCount + 1
ReDim Preserve labels(1 To LabelCount)
'Create the Label Listener objects
Set labels(LabelCount).classLabels = ctl
End If
Next ctl
End Sub
Function blinkThisControl(ctrl As Control, ByVal blinkCount As Long)
isClickDetected = False
Dim i As Integer
For i = 1 To blinkCount
' <blink ctrl twice>
DoEvents
If isClickDetected Then Exit Function
'name of clicked control will be in clickedControlName
Next i
End Function
Private Sub userform_initialize()
Call createFrameListeners
Call createLabelListeners
' do other stuff
End Sub