0

I have a Userform with several pages, one of which is called FXForward1 and if a certain condition is met I want to create a new page in the userform called FXForward2 that has all the same controls as FXForward1. Does anyone know how I can go about doing this? The following is the relevant part of the code I currently have and it's giving me the following error:

Run-Time Error '-2147319767(80028029)':

Could not paste the control. Invalid forward reference, or reference to uncompiled type

Sub UpdateForm()


Dim vbObject As Object
Dim objControl As Control
Dim OptionNumber As Integer


Set vbObject = ThisWorkbook.VBProject.VBComponents("UserForm1")
Set objControl = vbObject.Designer.Controls("MultiPage1")

' Other code here

If OptionNumber > 1 Then   
 objControl.Pages.Add ("FXForward" & OptionNumber)
 objControl.Pages(FXForward1).Controls.Copy
 objControl.Pages("FXForward" & OptionNumber).Paste  ' This line is where I get the error         

End If

UserForm1.Show

End sub
Community
  • 1
  • 1
  • Why on Earth are you accessing `UserForm1` from the VBIDE API? You know there's already a global-scope, default instance already pointing to that object, that you can access for free with the global `UserForm1` identifier? Its `MultiPage1` can be accessed with `UserForm1.MultiPage1`. What's up with the `Object` and `Control` types? That said, you shouldn't use that global-scope object, instead `Dim form As UserForm1` and then `Set form = New UserForm1` and work off that `form` object, so you won't affect the global instance. – Mathieu Guindon Jun 29 '16 at 21:10
  • Possible duplicate of [Copy Elements From One Page To Another in Multipage with VBA in Excel](http://stackoverflow.com/questions/10822450/copy-elements-from-one-page-to-another-in-multipage-with-vba-in-excel) – Mathieu Guindon Jun 29 '16 at 21:48
  • Worked like a charm -- thanks for your help! – rabbithair Jun 30 '16 at 11:28
  • Encountered another issue - when I copy all the controls and paste them to my new page it doesn't keep the same names. For example I had a ControlBox called "Currency", but when I pasted it to the new page it takes on a default name of "ControlBox25". Is there any way to have it take on the same name? – rabbithair Jun 30 '16 at 16:20
  • They're two different control instances. Two controls cannot have the same name/identifier in the same form, it would be a duplicate declaration... – Mathieu Guindon Jun 30 '16 at 16:22
  • You could copy the controls one by one and name them appropriately, e.g. call the `Currency` control `CurrencyBox1` in tab 1 and name it `CurrencyBox2` in tab 2 for example. – Mathieu Guindon Jun 30 '16 at 16:23
  • How do you copy them one by one? `ctl.copy` doesn't work for me (where `ctl` is a control type)... – rabbithair Jun 30 '16 at 17:10
  • hmm then you'll have to manually generate and position them one by one – Mathieu Guindon Jun 30 '16 at 17:11

0 Answers0