How to modify an existing line in a codemodule?

SeniorNewbie

Board Regular
Joined
Jul 9, 2023
Messages
77
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
  2. MacOS
Hi XLperts out there!
there's a module created by VBA named "mdl_DynARR". It shall contain dynamically created arrays with data samples. With my first result it looks like this:
VBA Code:
Option Explicit
Public aTest
Sub PopulateTest()
     aTest = Array("Test_1", "Test_2", "Test_3", "Test_4", "Test_5")
End Sub
created by:
Code:
Sub ArrayTest()
Dim sName As String
sName = "Test"
AddArray sName
End Sub

Sub AddArray(sName As String)
System 'defines some publics like workbook, ~sheets etc.
Dim sPublic As String, sArray As String, sItem As String, iLines As Integer, i As Integer
With wb.VBProject.VBComponents("mdl_DynARR").CodeModule
    If Left(.Lines(2, 1), 8) = "Public a" Then
        MsgBox "!"
        Exit Sub
    End If
End With
sPublic = "Public a" & sName
sArray = "     a" & sName & "=Array("""
For i = 1 To 5
    sItem = sName & "_" & i & """" & "," & """"
    sArray = sArray & sItem
Next i
sArray = Left(sArray, Len(sArray) - 2) & ")"

With wb.VBProject.VBComponents("mdl_DynARR").CodeModule
    iLines = .CountOfLines + 3
    .InsertLines iLines, sPublic: iLines = iLines + 1
    .InsertLines iLines, "Sub Populate" & sName & "()": iLines = iLines + 1
    .InsertLines iLines, sArray: iLines = iLines + 1
    .InsertLines iLines, "End Sub": iLines = iLines + 1
End With
End Sub
Don't ask how many hours I was fighting with this **ucked """""" to make this run ;-)

Okay - what's my question? In code paragraph one you can see a first result, in code paragraph two you see an if...then. That should trigger the next step: What I want to do is, to modify the code of paragraph one, line two to Public aTest, aTest2, aTest3 and so on. To add the next array is no problem. Modify is the subject

Is there any chance?

THX a lot! and enjoy the weekend!

Senior Newbie
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Okay - what's the answer? Do not modify - just replace!
VBA Code:
Sub AddArray(sName As String)
System
Dim sPublic As String, sArray As String, sItem As String, sLine As String, iLines As Integer, i As Integer
With wb.VBProject.VBComponents("mdl_DynARR").CodeModule
    If Left(.Lines(2, 1), 8) = "Public a" Then
        sLine = .Lines(2, 1)
        .ReplaceLine 2, Replace(.Lines(2, 1), sLine, sLine & ", a" & sName, , vbTextCompare)
    End If
End With
sPublic = "Public a" & sName
sArray = "     a" & sName & "=Array("""
For i = 1 To 5
    sItem = sName & "_" & i & """" & "," & """"
    sArray = sArray & sItem
Next i
sArray = Left(sArray, Len(sArray) - 2) & ")"

With wb.VBProject.VBComponents("mdl_DynARR").CodeModule
    iLines = .CountOfLines + 3
    .InsertLines iLines, sPublic: iLines = iLines + 1
    .InsertLines iLines, "Sub Populate" & sName & "()": iLines = iLines + 1
    .InsertLines iLines, sArray: iLines = iLines + 1
    .InsertLines iLines, "End Sub": iLines = iLines + 1
End With
End Sub

Have fun!
SeniorNewbie
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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