Write the items in an array to seperate lines in a codemodule

Vantom

Board Regular
Joined
Feb 28, 2014
Messages
65
I have this code:
Code:
Sub CreateImportModule()
    Dim CodeMod As VBIDE.CodeModule
    Dim x As Integer: x = 2
    Dim vArrModules() As Variant
    Dim VBProj As VBIDE.VBProject
    Dim VBCompModule As VBIDE.VBComponent
    Dim VBCompAddModule As VBIDE.VBComponent
    Dim y As Integer
    
        Set VBProj = ActiveWorkbook.VBProject
            For Each VBCompModule In VBProj.VBComponents
                ReDim Preserve vArrModules(y)
                vArrModules(y) = Chr(34) & VBCompModule.Name & ".bas" & Chr(34) & ", "
                y = y + 1
            Next VBCompModule
                
        Set VBCompModule = VBProj.VBComponents.Add(vbext_ct_StdModule)
            VBCompModule.Name = Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule"
        
        Set VBCompModule = VBProj.VBComponents(Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule")
 
        Set CodeMod = VBCompModule.CodeModule
            With CodeMod
                .InsertLines x, "    Dim strModuleName(): strModuleName = Array(" & Left(Join(vArrModules, ""), Len(Join(vArrModules, "")) - 2) & ")"
            End With
End Sub
Which results in this:
Code:
    Dim strModuleName(): strModuleName = Array("ThisWorkbook.bas", "Sheet1.bas", "Module1.bas", "Module2.bas", "Module3.bas")
But I want this:
Code:
    Dim strModuleName(): strModuleName = Array("ThisWorkbook.bas", _
                                                "Sheet1.bas", _
                                                "Module1.bas", _
                                                "Module2.bas", _
                                                "Module3.bas")
How?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I think this solves it:
Code:
Sub CreateImportModule()
    Dim CodeMod As VBIDE.CodeModule
    Dim x As Integer: x = 2
    Dim vArrModules() As Variant
    Dim VBProj As VBIDE.VBProject
    Dim VBCompModule As VBIDE.VBComponent
    Dim VBCompAddModule As VBIDE.VBComponent
    Dim y As Integer
    
        Set VBProj = ActiveWorkbook.VBProject
            For Each VBCompModule In VBProj.VBComponents
                ReDim Preserve vArrModules(y)
                vArrModules(y) = Chr(34) & VBCompModule.Name & ".bas" & Chr(34) & ",[COLOR=#ff0000] _[/COLOR]" [COLOR=#ff0000]& vbNewLine & Space(47)[/COLOR]
                y = y + 1
            Next VBCompModule
        [COLOR=#ff0000]vArrModules(UBound(vArrModules)) = Replace(vArrModules(UBound(vArrModules)), ", _", ")")[/COLOR]
                
        Set VBCompModule = VBProj.VBComponents.Add(vbext_ct_StdModule)
            VBCompModule.Name = Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule"
        
        Set VBCompModule = VBProj.VBComponents(Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule")
 
        Set CodeMod = VBCompModule.CodeModule
        
            With CodeMod
                .InsertLines x, "    Dim strModuleName(): strModuleName = Array(" & [COLOR=#ff0000]Join(vArrModules, "")[/COLOR]
            End With
End Sub
 
Upvote 0
And add the number of lines in the array, so the next inserted lines goes beneath the array:
Code:
Sub CreateImportModule()
    Dim CodeMod As VBIDE.CodeModule
    Dim x As Integer: x = 2
    Dim vArrModules() As Variant
    Dim VBProj As VBIDE.VBProject
    Dim VBCompModule As VBIDE.VBComponent
    Dim VBCompAddModule As VBIDE.VBComponent
    Dim y As Integer
    
        Set VBProj = ActiveWorkbook.VBProject
            For Each VBCompModule In VBProj.VBComponents
                ReDim Preserve vArrModules(y)
                vArrModules(y) = Chr(34) & VBCompModule.Name & ".bas" & Chr(34) & ",[COLOR=#ff0000] _[/COLOR]" [COLOR=#ff0000]& vbNewLine & Space(47)[/COLOR]
                y = y + 1
            Next VBCompModule
        [COLOR=#ff0000]vArrModules(UBound(vArrModules)) = Replace(vArrModules(UBound(vArrModules)), ", _", ")")[/COLOR]
                
        Set VBCompModule = VBProj.VBComponents.Add(vbext_ct_StdModule)
            VBCompModule.Name = Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule"
        
        Set VBCompModule = VBProj.VBComponents(Replace(ActiveWorkbook.Name, ".xlsm", "") & "ImportModule")
 
        Set CodeMod = VBCompModule.CodeModule
        
            With CodeMod
                .InsertLines x, "    Dim strModuleName(): strModuleName = Array(" & [COLOR=#ff0000]Join(vArrModules, "")
[/COLOR]                x = x + 1 [COLOR=#ff0000]+[/COLOR] [COLOR=#ff0000]UBound(vArrModules)[/COLOR]
            End With
End Sub
 
Upvote 0

Similar threads

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top