Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unresolved array error

Status
Not open for further replies.

UnsolvedCoding

Technical User
Jul 20, 2011
424
US

Why am I getting an error on line 100? The array is used in other subs without any problems and works fine everywhere else.


Sub New_Output_Array()

Dim RELAY As Boolean

On Error GoTo Error_Handler

100: Erase IO_OUTPUT_ARRAY

200: Erase RELAY_ARRAY

300: TEST = 1

400: COUNTER = 1

' Sets up starting row
500: A = 1

' Sets up starting column
600: C = 10

' Loop until the first cell in column B is empty
700: Do

' Go to the proper sheet
800: If IsEmpty(Sheets("Holding").Cells(A + 1, C).Value) Then Exit Do

' Count the number of cells holding a type
900: COUNTER = COUNTER + 1

' Get only the CR and CR4 count
1000: If Sheets("Holding").Cells(A + 1, C).Value = "CR" Or _
Sheets("Holding").Cells(A + 1, C).Value = "CR4" Then
1100: TEST = TEST + 1
1200: End If

' Go down one row
1300: A = A + 1

1400: Loop

' Set the variable
1500: I = 0

1600: J = 0

' Get ready to save the info
1700: ReDim IO_OUTPUT_ARRAY(3 * COUNTER)

1800: ReDim RELAY_ARRAY(3 * TEST)


1900: For A = 1 To COUNTER

2000: If Sheets("Holding").Cells(A, C).Value = "CR" Or _
Sheets("Holding").Cells(A, C).Value = "CR4" Then RELAY = True

' Put the nickname in the array
2100: IO_OUTPUT_ARRAY(I) = Sheets("Holding").Cells(A, C - 2).Value
2200: If RELAY = True Then _
RELAY_ARRAY(J) = Sheets("Holding").Cells(A, C - 2).Value

' Put the DESCRIPTION in the array
2300: IO_OUTPUT_ARRAY(I + 1) = Sheets("Holding").Cells(A, C - 1).Value
2400: If RELAY = True Then _
RELAY_ARRAY(J) = Sheets("Holding").Cells(A, C - 1).Value


' Put the TYPE in the array
2500: IO_OUTPUT_ARRAY(I + 2) = Sheets("Holding").Cells(A, C).Value

2600: If RELAY = True Then
2700: RELAY = False
2800: RELAY_ARRAY(J) = Sheets("Holding").Cells(A, C).Value
2900: J = J + 1

3000: End If

' Increase array by one
3100: I = I + 3

3200: Next A

3300: Exit Sub

Error_Handler:

Application.ScreenUpdating = True
FLAG = False
TEST2 = MsgBox("New_Output_Array Malfunctioned." & vbCrLf & _
"Error Number - " & Err.Number & vbCrLf & "Error Source - " & Err.Source & _
vbCrLf & "Error Description - " & Err.Description & _
vbCrLf & "Error Line: " & Erl & vbCrLf & "Help File - " & Err.HelpFile & _
vbCrLf & "Context - " & Err.HelpContext, vbOKOnly + vbCritical)

End Sub
 
First, please use TGML tags to present your code.
Secong, I don't see any:[tt]
Dim IO_OUTPUT_ARRAY() As Whatever[/tt]
Do you have [tt]Option Explicit[/tt] at the top of your code?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Hi,

If you have Option Explicit (which, BTW, you ought to have) then every variable that is not declared will give you an error.

In other words, you really ought to declare every single variable explicitly! It's what people do, as the GEICO guy says.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Sorry - forgot to mention those things.

All the code is option Explicit.

The arrays are public in a different module and used in a couple of modules.

Apparently if the array hasn't populated it can cause an error. When I did a test run of commenting out the erase array line the code ran. From what I can tell, if its the first time the code runs VBA doesn't always like the erase statement and can error out which is why there wasn't a problem with the other subs but there is for this one.
 
You can use IsArray to test if the array was initialised:

100: If IsArray(IO_OUTPUT_ARRAY) Then Erase IO_OUTPUT_ARRAY

combo
 
>You can use IsArray to test if the array was initialised

Er ... are you sure? That would only 'work' if IO_OUTPUT_ARRAY itself were to be defined as a Variant

 
Strange. In:
[tt]Dim MyArray() 'As Integer
Sub Test()
MsgBox IsArray(MyArray)
Erase MyArray
MsgBox IsArray(MyArray)
End Sub[/tt]
In all cases there is no error and the output is True. Writing my previous answer I had False for non-initialised array

combo
 
Well, as I said, it would only really work if <variable> was declared as a simple Variant. IsArray does not test for whether an array is initialised or not. It pretty much does exactly what the name implies - namely tests if the variable is an array or not. In the case of a simple variant, it is NOT an array (until an array is assigned to it):

I would hazard, given the Erase error, that the OPs variable is declared (within the same scope as this Sub) either as

Public IO_OUTPUT_ARRAY as Variant

or possibly

Public IO_OUTPUT_ARRAY

Mind you, not quite sure why the OP needs to Erase the arrays, since they are Redimmed before use anyway, which has the required erase effect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top