やりたい事は、下記のVBA「③ModuleImport」で一括インポートしたいのですが、
「実行エラー60061:読み中にエラー…」が発生して次に進む事ができません…!?
ログ内容→「行 2: フォーム名または MDI フォーム名 CalendarFormC2 は既に使われています。」
当該現象はリボン「Private Sub Ribbon_onLoad(ribbon As IRibbonUI)」を組込んだBookで発生。
勿論、事前に「②ModuleDelete」を実行して、次に「③ModuleImport」を実行すれば正常に作業は完結するのですが、これを連続処理する術が分かりません…連続処理は無理なのでしょうか?
解決方法をご教授よろしくお願いいたします。
マクロ実行場所
個人用マクロブック「PERSONAL.XLSB」に次の標準モジュールを投稿して運用します。
尚、元データは下記のVBA「①ModuleExport」で事前にエクスポートしてあるとします。
Option Explicit
Sub ①ModuleExport()
Dim wPath1 As String: wPath1 = "D:\VBC_Exports"
Dim VBC As VBIDE.VBComponent
Dim k As String
MsgBox "全モジュールをExportします。", 64
With ActiveWorkbook.VBProject
For Each VBC In .VBComponents
If VBC.Type = 1 Then k = ".bas"
If VBC.Type = 2 Then k = ".cls"
If VBC.Type = 3 Then k = ".frm"
If VBC.Type = 1 Or VBC.Type = 2 Or VBC.Type = 3 Then
If VBC.CodeModule.CountOfDeclarationLines <> VBC.CodeModule.CountOfLines Then
VBC.Export wPath1 & "\" & VBC.name & k
End If
End If
Next VBC
End With
MsgBox "◎◎◎処理完了◎◎◎", 64
End Sub
Sub ②ModuleDelete()
Dim VBC As VBIDE.VBComponent
With ActiveWorkbook.VBProject
For Each VBC In .VBComponents
If VBC.Type = 1 Or VBC.Type = 2 Or VBC.Type = 3 Then
.VBComponents.Remove VBC
End If
Next VBC
End With
End Sub
Sub ③ModuleImport()
Dim vPath As String: vPath = "D:\VBC_Exports\"
Dim sFilename As String
Dim vProject As Variant
MsgBox "全モジュールをインポートします。", 64
'----------------------
Call ②ModuleDelete '←これを事前に実行すると下記で★実行エラーが発生する
'----------------------
Set vProject = ActiveWorkbook.VBProject.VBComponents
sFilename = Dir(vPath & "*.*")
Dim k As Long
Do While sFilename <> ""
If Right(Trim(sFilename), 4) = ".bas" Or _
Right(Trim(sFilename), 4) = ".cls" Or _
Right(Trim(sFilename), 4) = ".frm" Then
k = k + 1
Debug.Print k & " " & vPath & sFilename
vProject.Import vPath & sFilename '←★実行エラー60061:読み中にエラーが発生…
End If
sFilename = Dir
Loop
MsgBox "◎◎◎処理完了◎◎◎", 64
End Sub