Using Arrays to record Cells
Hi All,
I'm trying to incoperate an array function into my script where when A condition is triggered it will save the contents of the selected cells in an array. Is this possible? This is what Ive got so far:
Code:
P = 2
Dim StudentName(i) As String
While Not IsEmpty(Cells(P, 1))
ActiveSheet.Range(Cells(2, 1), Cells(P, 11)).Select
If Cells(P, 11) <= 0 Then
StudentName(i) = Cells(P, 1)
InputBox (StudentName(i))
End If
P = P + 1
Wend
InputBox (StudentName(1))
Now, the InputBox (StudentName(i)) works inside the if statement, but the InputBox (StudentName(1)) doesnt.
Anyone got ant ideas?
Cheers,
Matt
Re: Using Arrays to record Cells
well as an inputbox is designed to get input from the user, i can not figure what you are trying to achieve, or what this has to do with the question about an array
Re: Using Arrays to record Cells
Hi,
The input boxes are being used to display the number, simply just to show that my loop and conditional statement is working.
SO, what ive got in my spread sheet is this:
1
2
3
i'm getting the
Code:
InputBox (StudentName(i))
to display each number in as the if statment is triggered.
However, I assumed by using
Code:
InputBox (StudentName(1))
after the condition, it would display the number 1 as that was the first integer in the array.
Do arrays work like this?
Cheers for your help,
Matt
Re: Using Arrays to record Cells
Quote:
The input boxes are being used to display the number
probably a msgbox would be better for this purpose
i am not actually sure why your code works at all
Quote:
Dim StudentName(i) As String
an array has to be declared with a literal or constant value, not a variable,
or with empty brackets, for a dynamic array, then a redim statement is required to initialise the array, or else the array must be filled from some function or assigned a value from other array (or range of cells)
Quote:
If Cells(P, 11) <= 0 Then
StudentName(i) = Cells(P, 1)
as i is never incremented the same element in the array is always assigned the value from the cell