Copying controls in a Multipage form without copying form values

H_gupta

New Member
Joined
Mar 26, 2019
Messages
33
Hi,

I have created a Multipage userform, with 4 Text Boxes.
I am using a button to add a New page and copy the controls from the First page dynamically
But this code also copies the data entered in the new page. Is there any way I can copy only the form, with no values ?
Below is code i am using.

Requirement : The new pages added should be empty with no values in the text boxes


Private Sub CommandButton1_Click()


Dim l As Double, r As Double
Dim ctl As Control

If x = 19 Then
MsgBox ("You can add only 20 Level 4 Categories")

ElseIf x <> 19 Then
MultiPage1.Pages.Add ("Level 4-" & x + 2)
x = x + 1
i = i + 1
MultiPage1.Pages(0).Controls.Copy
MultiPage1.Pages(x).Paste

End If

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Your code has been amended so that the following controls will be cleared...

Code:
CheckBox
OptionButton
ComboBox
TextBox
ListBox

If you have a listbox and MultiSelect is set to True, the code will need to be amended. Also, if you have other controls that are being copied, you'll also need to amend the code accordingly. Here's the code...

Code:
Private Sub CommandButton1_Click()

    Dim l As Double, r As Double
    Dim ctl As Control
    
    If x = 19 Then
        MsgBox ("You can add only 20 Level 4 Categories")
    ElseIf x <> 19 Then
        MultiPage1.Pages.Add ("Level 4-" & x + 2)
        x = x + 1
        i = i + 1
        MultiPage1.Pages(0).Controls.Copy
[COLOR=#ff0000]        With MultiPage1.Pages(x)[/COLOR]
[COLOR=#ff0000]            .Paste[/COLOR]
[COLOR=#ff0000]            For Each ctl In .Controls[/COLOR]
[COLOR=#ff0000]                Select Case TypeName(ctl)[/COLOR]
[COLOR=#ff0000]                    Case "CheckBox", "OptionButton": ctl.Value = False[/COLOR]
[COLOR=#ff0000]                    Case "ComboBox", "TextBox": ctl.Value = ""[/COLOR]
[COLOR=#ff0000]                    Case "ListBox": ctl.ListIndex = -1[/COLOR]
[COLOR=#ff0000]                End Select[/COLOR]
[COLOR=#ff0000]            Next ctl[/COLOR]
        End With
    End If


End Sub

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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