Array of WorkSheets, CodeModule to Handle each WorkSheet

VisLord

New Member
Joined
Apr 27, 2002
Messages
30
I'm trying to create an dynamic array of worksheets, place a commandbutton on each worksheet, write marco code via .Insertlines in a seperate CodeMolule to handle each commandbutton on each worksheet in the array.

All works well, until I .Insertlines and then I loose the reference (scope??) to the main array of worksheets.
So the created Marco doesn't work (no array)

Actual code can be posted...

Help again, guys
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi TsTome,

I'm declaring the array as a global in Module1, "redim"ing as I go along.

Option Explicit
Option Base 1
Dim wS() As Worksheet, uF As VBComponent, pt As Integer
....code....
If pt = 1 Then ReDim wS(pt) Else ReDim Preserve wS(pt)
Sheets.Add after:=Worksheets(Worksheets.Count)
Set wS(pt) = ActiveSheet
....code....

The CodeModule is added in the ThisWorkbook Module

ThisWorkbook.VBProject.VBComponents.Add(1).Name = "cM"

The following sub is ran in Module1 (with NO reference to the array)-

Sub softM()
Dim sSub As CodeModule, l As Integer
Set sSub = ThisWorkbook.VBProject.VBComponents("cM").CodeModule
With sSub
l = .CountOfLines
.InsertLines l + 1, "Sub Scope_" & pt & "()"
.InsertLines l + 2, " setMM(" & pt & ")"
.InsertLines 1 + 3, "End Sub" & Chr(13)
End With
End Sub

After the sub runs the array has no variables.

If I comment out the ".InsertLines" statements, the sub works OK, i.e. I can count the lines and retain the array.

Tks...
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,978
Latest member
rrauni

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