我正在使用均使用相同用户窗体和模块的工作簿。我已经将这些用户表单和模块导出到共享驱动器上的文件夹中。
我想遍历文件夹中的每个工作簿,删除现有的模块和用户表单,然后从本地文件夹中导入选定的用户表单和模块。
这是代码
Sub UpdateAllSheets()
'//THIS SUB REMOVES ALL USERFORMS AND MODULES FROM EACH WORKBOOK IN A
SPECIFIED DESTINATION FOLDER
'//UPDATED VERSIONS OF EACH OF THESE USERFORMS AND MODULES ARE THEN IMPORTED FROM
Dim MyPath As String 'Pathway where files are stored
Dim MyFiles() As String 'Array where files in path are stored
Dim wbarr() As Object 'Array for workbooks
Dim Element As Object
MyPath = "S:\Water TCM and Outreach\Data\Follow-Up\Daily Follow-Up (CM and FN Use)"
MyFiles = FindFilesinPath(MyPath)
Call OpenallFiles(MyPath, MyFiles)
wbarr = AssignBooks(MyFiles)
'Loop through all workbooks
For i = LBound(wbarr) To UBound(wbarr)
'Steps for this process:
Debug.Print "Updating Elements in "; wbarr(i).name
'Remove all existing Userforms and Modules from target workbook
For Each Element In wbarr(i).VBProject.VBComponents
wbarr(i).VBProject.VBComponents.Remove Element
Next
'Add updated Userforms and Modules from Destination folder
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\CalendarForm.frm"
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\CaseType.frm"
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\NewCase.frm"
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\NewEncounterShared.frm"
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\ModifyCase.frm"
wbarr(i).VBProject.VBComponents.Import "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\CaseManagementPreReqs.bas"
'Delete lines on "ThisWorkbook" for each workbook and import text from 'ThisWorkbook' cls file
With wbarr(i).VBProject.VBComponents("ThisWorkbook").CodeModule
.DeleteLines StartLine:=1, Count:=.CountOfLines
.AddFromFile "S:\Water TCM and Outreach\Data\Follow-Up\Admin\CM UserForms and Modules\ThisWorkbook.cls"
End With
Next i
'Move on to the next worksheet
MsgBox ("Update Process Complete: Fingers Crossed!")
End Sub
我遇到的问题是:
For Each Element In wbarr(i).VBProject.VBComponents
wbarr(i).VBProject.VBComponents.Remove Element
Next
我收到以下错误:运行时错误'5':无效的过程调用或参数。
我认为这与尝试使用工作簿在数组中的位置来修改工作簿有关。也许我可以为工作簿名称创建一个保存变量,然后使用Workbooks(TempName)或类似的东西。
这里还没有显示其他几个功能。我不相信它们有任何错误,因为我运行的另一个宏使用相同的功能来筛选目标文件夹中的所有工作簿。
答案 0 :(得分:1)
要仅删除用户表单和 standard 模块,可以测试每个Element的类型...
For Each Element In wbarr(i).VBProject.VBComponents
If Element.Type = 1 Or Element.Type = 3 Then '1 = vbext_ct_StdModule; 3 = vbext_ct_MSForm
wbarr(i).VBProject.VBComponents.Remove Element
End If
Next