The error you’re encountering, “Sub or function not defined,” typically occurs in VBA when you’re trying to call a procedure, function, or method that VBA does not recognize. This could be due to a typo, a missing reference, or in your case, an incorrect way of assigning a value to an array element.
In VBA, assigning a value to an array element is done using the syntax ArrayName(Index) = Value, without the Set keyword for non-object data types. However, the Set keyword is required when you are assigning an object to a variable. Given your code snippet, it looks like you are trying to assign a shape object (c) to an array (crtl_coords), which should store object references.
The error line Set crtl_coords(i) = c seems to intend to store the shape object c in the array at position i. The syntax you’ve used is slightly incorrect for this operation. If crtl_coords is meant to hold objects (in this case, Shape objects), ensure that crtl_coords is properly declared to store objects. If it’s an array of objects, your syntax should still work with the Set keyword, assuming crtl_coords has been correctly dimensioned or initialized to hold object references.
Make sure that:
-
crtl_coords is declared as an array capable of holding object references. This could be done using a dynamic array with Dim and later ReDim, or by declaring an array size initially if the size is known.
Example for a dynamic array:
Dim crtl_coords() As Shape
ReDim crtl_coords(1 To SomeSize) ' SomeSize being the size you need
-
You’re using the Set keyword correctly, as your line of code seems correct for assigning an object to an array element. If crtl_coords is not initially declared to hold Shape objects, you will face issues.
If after ensuring the above, you’re still facing the error, double-check the declaration of crtl_coords and how it’s being managed in your code. If it’s not declared at all or not declared as an array capable of holding objects, you would encounter such an error.
Also, ensure that you have declared i and it’s being correctly incremented or managed in your loop (assuming this code is within a loop), and that crtl_coords is correctly dimensioned to include the i index you’re using.