Public Array in a form?

L

lux770

Hi,

I am trying to create a user form that toggles the spreadsheet vie
between the normal cells interior colors and colors that depend on th
cell content (e.g. yellow for a formula etc.)
I have created the form and the procedures that actually perform th
actions. My problem is that when loading the form, I want to store th
color of each cell of the UsedRange in an bi-dimensional array, so tha
I can restore the original colors of the spreadsheet (making sense s
far? :confused: )
That array needs to be public because I will need to use it in severa
procedures such as togglebutton1_change, userform_terminate etc
Therefore I have tried to delare it at the top of the form's code as
public variable.

Unfortunately I get the following message: "-Constants, fixed-lengt
strings, arrays, user-defined types, and Declare statements not allowe
as Public members of an object module-"

The VBA help suggests to "-use a set of Property procedures that accep
and return a Variant containing an array-", but I am afraid I have no
reach that level of competency yet. :( Could someone explain to me i
detail what I should do?
Many thanks
 
T

Tom Ogilvy

Declare it as public in a general module and it will be visible/usable in
all modules.
 
L

lux770

Thanks Tom, I have applied what you suggest and the form now loads
correctly.
Unfortunatelu as soon as I press one of the toggle buttons, I get the
following message:
-"Error: Array or user-defined type expected"-

To give you some more details, I have created a procedure that calls
that form in a new module. At the top of that module I have declared my
array of integers as Public.

This is the code for the toggle button and the procedure that is
called:


Code:
--------------------
Private Sub tglOriginal_Change()

If tglOriginal.Value = True Then
Restore_BkGrnd ActiveSheet.UsedRange, iCellsColors
End If

End Sub

Private Sub Restore_BkGrnd(rngUsed As Range, iColorsArr() As Variant)

Dim lMaxRows, lMaxCol As Long
Dim l_Row, l_Col As Long

lMaxRows = rngUsed.Rows.Count
lMaxCol = rngUsed.Columns.Count

For l_Row = 1 To lMaxRows
For l_Col = 1 To lMaxCol
rngUsed.Cells(l_Row, l_Col).Interior.ColorIndex = iColorsArr(l_Row - 1, l_Col - 1)
Next l_Col
Next l_Row

End Sub
--------------------


It looks to me that calling the form works because it is done from the
module where the array is declared, but then all actions within the
form are performed from the form module, where there is no declaration
for that array. Or am I missing something?

Many thanks for your help.
 
T

Tom Ogilvy

I assume you are using xl2000 or later. I had this at the top of a general
module:
Public iCellsColors() As Variant
Public bNormal As Boolean

Sub Showform()
Dim rng As Range
bNormal = True
Set rng = ActiveSheet.UsedRange
ReDim iCellsColors(1 To rng.Rows.Count, _
1 To rng.Columns.Count)
For i = 1 To UBound(iCellsColors, 1)
For j = 1 To UBound(iCellsColors, 2)
iCellsColors(i, j) = Cells(i, j).Interior.ColorIndex
Next
Next
UserForm1.Show

End Sub

In a userform Module I had basically your code slightly modified to act as a
toggle:

' I made tglOriginal a commandbutton and used the boolean variable to
control the toggle.

Private Sub tglOriginal_Click()

If bNormal Then
For Each cell In ActiveSheet.UsedRange
cell.Interior.ColorIndex = Int(Rnd() * 56 + 1)
Next
bNormal = False
Else
Restore_BkGrnd ActiveSheet.UsedRange, iCellsColors
bNormal = True
End If

End Sub

Private Sub Restore_BkGrnd(rngUsed As Range, iColorsArr() As Variant)

Dim lMaxRows, lMaxCol As Long
Dim l_Row, l_Col As Long

lMaxRows = rngUsed.Rows.Count
lMaxCol = rngUsed.Columns.Count

For l_Row = 1 To lMaxRows
For l_Col = 1 To lMaxCol

' my array was 1 based, so I removed the -1 in iColorsArr

rngUsed.Cells(l_Row, l_Col).Interior.ColorIndex = iColorsArr(l_Row, l_Col)
Next l_Col
Next l_Row

End Sub

this worked fine for me.
 
L

lux770

It works!!!!
Apparently the key was to populate the array from the general modul
code, not the form module.

I have made a few more change to make the form modeless and preven
errors when the user switches to another sheet/workbook.

Thank you very much indeed, you've been very helpful.
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Public array 1
Maintaing public array values in a user form 1
Declare a Public Array 5
Array assignment 1
Using a Public Array 4
Public Array 5
Public variable defintion (newbie) 3
Global Array in User Form 1

Top