我想做的是使用VBA for Excel删除用户表单并将其替换为更新的用户表单。
我导出了原始的用户窗体,进行了更改,现在想用新的用户窗体替换原始的用户窗体。两种用户表单具有相同的名称。我希望通过保持用户窗体名称相同,激活用户窗体的按钮不会受到损坏。
我要让VBA遍历所有工作簿,并执行约3200次。
Sub Macro2()
'''delete and replace a UserForm
'Dim some stuff
Application.ScreenUpdating = False
Dim SourceRow As Long
Dim EditRow As Long
Dim sFile As String
Dim wb As Workbook
Dim FileName1 As String
Dim FileName2 As String
Dim wksSource As Worksheet
Const scWkbSourceName As String = "theFILE.xlsm"
Set wkbSource = Workbooks(scWkbSourceName)
Set wksSource = wkbSource.Sheets("Sheet1")
Const wsOriginalBook As String = "theFILE.xlsm"
Const sPath As String = "C:\examplefolder\" 'this is PATH
SourceRow = 5
''ENSURE SELECT SOURCE SHEET
Sheets("Sheet1").Select
Do While Cells(SourceRow, "C").Value <> ""
FileName1 = wksSource.Range("A" & SourceRow).Value
FileName2 = wksSource.Range("L" & SourceRow).Value
EditRow = 2
sFile = sPath & FileName1 & "\" & FileName2 & ".xlsm"
Set wb = Workbooks.Open(sFile)
''DIM OPEN WORKBOOK
Dim AWorkbook As Workbook
Set AWorkbook = ActiveWorkbook
With AWorkbook.VBProject.VBComponents
.Remove.Item ("NewJobEvent")
End With
''CLOSE WORKBOOK W/O BEFORE SAVE
Application.EnableEvents = False
ActiveWorkbook.Save
Application.EnableEvents = True
ActiveWorkbook.Close
Windows("theFILE.xlsm").Activate
Sheets("Sheet1").Select
SourceRow = SourceRow + 1 ' Move down 1 row for source sheet
Loop
End Sub
任何人以及所有帮助和指针都将在此之前得到极大的赞赏。
答案 0 :(得分:2)
发件人:http://www.cpearson.com/excel/vbe.aspx
Sub DeleteModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
VBProj.VBComponents.Remove VBComp
End Sub
Remove
接受模块引用,而不是字符串
要从文件导入模块,请执行以下操作:
ActiveWorkbook.VBProject.VBComponents.Import FullPathHere