Results 1 to 4 of 4

Thread: Using Arrays to record Cells

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    18

    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
    Twitter @dancematt

    Programmers unite

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    18

    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
    Twitter @dancematt

    Programmers unite

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Using Arrays to record Cells

    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
    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)

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width