Copy userform to another workbook

纵然是瞬间 提交于 2020-07-07 10:41:42

问题


I've had a look for an answer to this question but can't find anything that matches exactly what I'm looking for, if someone has the answer or can point me in the direction of a question which answers this, it would be much appreciated.

I've been talked with building a solution to enter data into a workbook and have successfully built it, however the workbook it need to be added to is a very active tool and is constantly being updated with new/changed data, so I have had to build it in a copy of the workbook and now need to add it into the active workbook. I know an easy way would be to simply grab the data out of the active workbook and add it into my version and then make my version the active book, however this would be a fairly large undertaking and I can only take the workbook offline for ~5 minutes so I figured the easier way would be to copy my code and userform into the active spreadsheet. The code is easy enough to copy and paste in, but I can't find a way to copy the userform (formatting and all) into the workbook, is there a way to export it and then import it into the live workbook?

I hope this is clear and easy to understand, please let me know if I need to clarify anything.


回答1:


You can open code editor for both Excel sheets. Now drag and drop userform from one Excel to another Excel. Thanks




回答2:


i will show you how you can export & import a Userform in VBA , and then re-import it in an other workbook:

Option Explicit 

Public Function CopyUserForm(ByVal FormName$, Optional ByVal WB_Dest As Workbook) As Workbook   'copies sheets/Thisworkbook/Userforms/Modules/Classes  to a new workbook

Dim Comp As VBComponent
Dim CompStr$

On Error Resume Next 'needed for testing if component already exists in destination WorkBook, and vbe minimizing

If WB_Dest Is Nothing Then Set WB_Dest = Application.Workbooks.add

For Each Comp In ThisWorkbook.VBProject.VBComponents
    With Comp

            If .Type = vbext_ct_MSForm Then '=3

                If .Name = FormName Then

                    '// Export Form
                    CompStr = "C:\" & .Name ' & " " & Replace(Date, "/", "-") & ".frm"
                    .Export FileName:=CompStr 'this line fails if the destination Disk is protected, wich happened on my system disk :/

                    '// Import Form to new workbook
                    WB_Dest.VBProject.VBComponents.Import FileName:=CompStr

                    '// Kill temporary Form Files
                    Kill CompStr: Kill CompStr & ".frx"

                    Exit For

                End If

            End If

    End With 'comp

Next Comp

Err.Clear: On Error GoTo 0

Set CopyComponentsModules = WB_Dest

Set Comp = Nothing
Set WB_Dest = Nothing

End Function

You can then call the function like this: CopyUserForm "UserForm1" , for example.



来源:https://stackoverflow.com/questions/44479289/copy-userform-to-another-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!