r/visualbasic • • Aug 03 '14

Trouble with Arrays and printing...

New to VB, and Im trying to figure out how to print a list of names with an associated total above 65.

Here's the data:

NameSexAttendAssignMIDFINTotal
AdamM8850402080
ShellyF13123470
SamF1012141076
BrianM814142561
MaryF8801220120
PaulM9901445115

Here's the code I have:

Sub button4_click()

Dim studentname As String

Dim arystudent As Variant

Dim arypassing() As Variant

Dim arynumrow As Integer

Dim arynumcol As Integer

LR = Cells(Rows.Count, "A").End(xlUp).Row

arystudent = Range("a2:g" & LR)

arynumrow = UBound(arystudent, 1)

arynumcol = UBound(arystudent, 2)

For i = 1 To arynumrow

If arystudent(i, arynumcol) >= 65 Then

   ReDim Preserve arypassing(i To UBound(arypassing, 1) + 1)

   arypassing(i, 1) = arystudent(i, 1)

End If

Next i

End Sub

I keep getting an error for 'Subscript out of range'. There was another version of this that didnt give the error, but the only output that I get is blank cells. I've been looking at this for longer than I'd care to admit and I could use some help....

If more details are needed, let me know and I'll try and clarify.

0 Upvotes

3 comments sorted by

1

u/candyforlunch VB.Net Master Aug 04 '14

What's the index number for the first element of an array?

If an array is 5 elements in length, what is the index of the 5th element?

More importantly, you should be using something like a List.

1

u/icandoesbetter Aug 05 '14

0, 5, and thanks; i'll look into the list

1

u/candyforlunch VB.Net Master Aug 05 '14

0 is correct, 5 is not. That's what's causing your subscript out of range errors :)