Compile error with collection

Xmarksthespot

New Member
Joined
Jan 21, 2017
Messages
7
I have a sub whereI declare and set a collection (Cat_colw) which consists of a number of 3x3 arrays.<o:p></o:p>
I have a set ofvariables (gp1,gp2,gp3,…) that represent a 3x3 range of cells.<o:p></o:p>
I use thestatement: gp2.Value = Cat_colw(1) , to enterthe values of the array Cat_colw(1) intothe range gp2.<o:p></o:p>
This works fineif I do this in the same sub, but when I put this statement in a different suband make a call to it I get the error ------ <o:p></o:p>
<o:p> </o:p>
Compile Error<o:p></o:p>
Sub or Functionnot defined.<o:p></o:p>
<o:p> </o:p>
I’m a completebeginner so please excuse me if this is such a trivial task<o:p></o:p>
Thank you
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Variables declared within one procedure or function are generally not available to other procedures unless they are passed (best option, generally underused) or declared at a higher (eg module) level. Have a read of this

Understanding Scope
 
Upvote 0
Please include the offending code, using [code]'...your code here[/code] like:
Code:
'...your code here
 
Upvote 0
Variables are declared "local" within a sub which means they can't be accessed from other subs unless you pass them as parameters. Move the declaration of Cat_colw to the top of the module above all sub definitions and that will make it available to other subs in that module. E.g.:

Code:
Dim Cat_colw As New Collection
Sub sub1()
'...
' You can use Cat_colw here
'...
End Sub
Sub sub2()
'...
' You can use Cat_colw here as well
'...
End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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