Adding multiple columns of of multiple row values into an array of custom type

时间:2019-01-09 22:02:57

标签: excel vba

I have a worksheet that tracks student test scores and gpa information. The worksheet layout is as follows:

Student ID, Name, Test 1 Score, Test 2 Score, Final Exam Score, GPA and Final Score. There are 20 rows.

I need to get the student id, whether or not they took the final exam, the gpa and the final score and store them in an array of custom type. Then there will be a form where a student inputs their id and it retrieves the appropriate data from the array.

I put a sample two row table in and the vba code I have so far.

My question is how do i get this data into the Array?

I am totally confused at this point....

<div id="good_5932" align=center x:publishsource="Excel">

<table border=0 cellpadding=0 cellspacing=0 width=611 class=xl635932
 style='border-collapse:collapse;table-layout:fixed;width:458pt'>
 <col class=xl635932 width=74 style='mso-width-source:userset;mso-width- 
 alt:
 2645;width:56pt'>
 <col class=xl635932 width=100 style='mso-width-source:userset;mso-width- 
 alt:
 3555;width:75pt'>
 <col class=xl635932 width=76 style='mso-width-source:userset;mso-width- 
 alt:
 2702;width:57pt'>
 <col class=xl635932 width=77 style='mso-width-source:userset;mso-width- 
 alt:
 2730;width:58pt'>
 <col class=xl635932 width=152 style='mso-width-source:userset;mso-width- 
 alt:
 5404;width:114pt'>
 <col class=xl635932 width=66 style='width:49pt'>
 <col class=xl695932 width=66 style='width:49pt'>
 <tr height=37 style='height:27.6pt'>
  <td height=37 class=xl655932 width=74 
  style='height:27.6pt;width:56pt'>Student
  ID</td>
  <td class=xl655932 width=100 style='border-left:none;width:75pt'>Student 
  Name</td>
  <td class=xl665932 width=76 style='border-left:none;width:57pt'>Test 1 
  Score</td>
  <td class=xl665932 width=77 style='border-left:none;width:58pt'>Test 2 
  Score</td>
  <td class=xl665932 width=152 style='border- 
 left:none;width:114pt'>Comprehensive
  Final</td>
  <td class=xl665932 width=66 style='border-left:none;width:49pt'>Student 
  GPA</td>
  <td class=xl685932 width=66 style='border-left:none;width:49pt'>Final 
  Score</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>5</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Joe<span
  style='mso-spacerun:yes'> </span></td>
  <td class=xl645932 style='border-top:none;border-left:none'>78</td>
  <td class=xl675932 style='border-top:none;border-left:none'>12</td>
  <td class=xl645932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.5</td>
  <td class=xl675932 style='border-top:none;border-left:none'>96</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>7</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Hans</td>
  <td class=xl645932 style='border-top:none;border-left:none'>80</td>
  <td class=xl675932 style='border-top:none;border-left:none'>98</td>
  <td class=xl645932 style='border-top:none;border-left:none'>98</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.7</td>
  <td class=xl675932 style='border-top:none;border-left:none'>98</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>8</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Ferris</td>
  <td class=xl645932 style='border-top:none;border-left:none'>92</td>
  <td class=xl675932 style='border-top:none;border-left:none'>56</td>
  <td class=xl645932 style='border-top:none;border-left:none'>51</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3</td>
  <td class=xl675932 style='border-top:none;border-left:none'>66</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>10</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Mike</td>
  <td class=xl645932 style='border-top:none;border-left:none'>45</td>
  <td class=xl675932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>2.6</td>
  <td class=xl675932 style='border-top:none;border-left:none'>87</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>15</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Lauren</td>
  <td class=xl645932 style='border-top:none;border-left:none'>98</td>
  <td class=xl675932 style='border-top:none;border-left:none'>49</td>
  <td class=xl645932 style='border-top:none;border-left:none'>89</td>
  <td class=xl645932 style='border-top:none;border-left:none'>4</td>
  <td class=xl675932 style='border-top:none;border-left:none'>92</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
 top:none'>62</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Larry</td>
  <td class=xl645932 style='border-top:none;border-left:none'>75</td>
  <td class=xl675932 style='border-top:none;border-left:none'>85</td>
  <td class=xl645932 style='border-top:none;border-left:none'>65</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.9</td>
  <td class=xl675932 style='border-top:none;border-left:none'>75</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
 top:none'>68</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Tony</td>
  <td class=xl645932 style='border-top:none;border-left:none'>48</td>
  <td class=xl675932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>89</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.5</td>
  <td class=xl675932 style='border-top:none;border-left:none'>96</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
 top:none'>85</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Kevin</td>
  <td class=xl645932 style='border-top:none;border-left:none'>68</td>
  <td class=xl675932 style='border-top:none;border-left:none'>83</td>
  <td class=xl645932 style='border-top:none;border-left:none'>79</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.2</td>
  <td class=xl675932 style='border-top:none;border-left:none'>80</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>98</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Sam</td>
  <td class=xl645932 style='border-top:none;border-left:none'>59</td>
  <td class=xl675932 style='border-top:none;border-left:none'>62</td>
  <td class=xl645932 style='border-top:none;border-left:none'>98</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.6</td>
  <td class=xl675932 style='border-top:none;border-left:none'>87</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
 top:none'>101</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Zibin</td>
  <td class=xl645932 style='border-top:none;border-left:none'>63</td>
  <td class=xl675932 style='border-top:none;border-left:none'>68</td>
  <td class=xl645932 style='border-top:none;border-left:none'>95</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.5</td>
  <td class=xl675932 style='border-top:none;border-left:none'>88</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>105</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Ran</td>
  <td class=xl645932 style='border-top:none;border-left:none'>49</td>
  <td class=xl675932 style='border-top:none;border-left:none'>59</td>
  <td class=xl645932 style='border-top:none;border-left:none'>94</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.8</td>
  <td class=xl675932 style='border-top:none;border-left:none'>85</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>108</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Donia</td>
  <td class=xl645932 style='border-top:none;border-left:none'>87</td>
  <td class=xl675932 style='border-top:none;border-left:none'>12</td>
  <td class=xl645932 style='border-top:none;border-left:none'>92</td>
  <td class=xl645932 style='border-top:none;border-left:none'>2.9</td>
  <td class=xl675932 style='border-top:none;border-left:none'>104</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>109</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Kate</td>
  <td class=xl645932 style='border-top:none;border-left:none'>85</td>
  <td class=xl675932 style='border-top:none;border-left:none'>57</td>
  <td class=xl645932 style='border-top:none;border-left:none'>49</td>
  <td class=xl645932 style='border-top:none;border-left:none'>2.6</td>
  <td class=xl675932 style='border-top:none;border-left:none'>64</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>110</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Sahar</td>
  <td class=xl645932 style='border-top:none;border-left:none'>45</td>
  <td class=xl675932 style='border-top:none;border-left:none'>59</td>
  <td class=xl645932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>2.7</td>
  <td class=xl675932 style='border-top:none;border-left:none'>81</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>115</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Milos</td>
  <td class=xl645932 style='border-top:none;border-left:none'>21</td>
  <td class=xl675932 style='border-top:none;border-left:none'>49</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.8</td>
  <td class=xl675932 style='border-top:none;border-left:none'>35</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>125</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Brian</td>
  <td class=xl645932 style='border-top:none;border-left:none'>49</td>
  <td class=xl675932 style='border-top:none;border-left:none'>98</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.9</td>
  <td class=xl675932 style='border-top:none;border-left:none'>74</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
 top:none'>130</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Zane</td>
  <td class=xl645932 style='border-top:none;border-left:none'>34</td>
  <td class=xl675932 style='border-top:none;border-left:none'>38</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.2</td>
  <td class=xl675932 style='border-top:none;border-left:none'>36</td>
 </tr>

 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>145</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Istiak</td>
  <td class=xl645932 style='border-top:none;border-left:none'>91</td>
  <td class=xl675932 style='border-top:none;border-left:none'>87</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3.1</td>
  <td class=xl675932 style='border-top:none;border-left:none'>89</td>

 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>150</td>
  <td class=xl645932 style='border-top:none;border-left:none'>Kumari</td>
  <td class=xl645932 style='border-top:none;border-left:none'>100</td>
  <td class=xl675932 style='border-top:none;border-left:none'>78</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>3</td>
  <td class=xl675932 style='border-top:none;border-left:none'>89</td>
 </tr>
 <tr height=19 style='height:14.4pt'>
  <td height=19 class=xl645932 style='height:14.4pt;border- 
  top:none'>186</td>
  <td class=xl645932 style='border-top:none;border-left:none'>John</td>
  <td class=xl645932 style='border-top:none;border-left:none'>19</td>
  <td class=xl675932 style='border-top:none;border-left:none'>76</td>
  <td class=xl645932 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl645932 style='border-top:none;border-left:none'>2.9</td>
  <td class=xl675932 style='border-top:none;border-left:none'>48</td>
 </tr>
 <![if supportMisalignedColumns]>
 <tr height=0 style='display:none'>
  <td width=74 style='width:56pt'></td>
  <td width=100 style='width:75pt'></td>
  <td width=76 style='width:57pt'></td>
  <td width=77 style='width:58pt'></td>
  <td width=152 style='width:114pt'></td>
  <td width=66 style='width:49pt'></td>
  <td width=66 style='width:49pt'></td>
 </tr>
 <![endif]>
</table>

</div>

Option Explicit

'***** MyType as an array
Public Type Student
StudentID As Integer ' Student ID
FinalScore As Integer ' Studnts final score
ComprehensiveTaker As Integer ' Comprehensivetaker
StudentGPA As Double ' Student's GPA
End Type

Public CEEStudent(20) As Student

Public Sub fillCEEStudent()

    Dim j As Long, i As Long, CEEStudent(20) As Student
            j = 0
            i = 3

          If j < 21 And i < 23 Then
            j = j
            i = i

            CEEStudent(j).StudentID = Sheets("Question1").Cells(i, 3).Value
            CEEStudent(j).ComprehensiveTaker = IIf(Sheets("Question1").Cells(i, 7).Value <> "", 1, 0)
            CEEStudent(j).StudentGPA = Sheets("Question1").Cells(i, 8).Value
            CEEStudent(j).FinalScore = Sheets("Question1").Cells(i, 9).Value
          Else:
            j = 0
            i = 3

         End If

    Dim formNum As Integer, formScore As Integer, formGPA As Double, formInput As String

    With VBAProject.UserForm1

        formInput = 5 'VBAProject.UserForm1.TextBox1.Value

        If IsNumeric(formInput) Then
            formNum = formInput
        Else
            formNum = 0
        End If

        If formInput <> "" And formNum > 0 Then
                If CEEStudent(j).StudentID > formNum Or CEEStudent(j).StudentID < formNum Then
                    VBAProject.UserForm1.TextBox2.Text = CStr(" ")
                    VBAProject.UserForm1.BackColor = vbRed
                    VBAProject.UserForm1.StudentID.BackColor = vbRed
                    VBAProject.UserForm1.TestScore.BackColor = vbRed
                    VBAProject.UserForm1.GPA.BackColor = vbRed
                    VBAProject.UserForm1.TextBox2.BackColor = vbRed
                    VBAProject.UserForm1.TextBox3.BackColor = vbRed
                Else:
                    VBAProject.UserForm1.TextBox2.Text = CStr(CEEStudent(j).FinalScore)
                    VBAProject.UserForm1.TextBox3.Text = CStr(CEEStudent(j).StudentGPA)
                End If
        Else:
            formScore = 0
        End If

        VBAProject.UserForm1.TextBox2.Text = CStr(formScore)
        VBAProject.UserForm1.TextBox3.Text = CStr(formGPA)

        If VBAProject.UserForm1.TextBox2.Text >= 95 Then
           VBAProject.UserForm1.BackColor = vbGreen
           VBAProject.UserForm1.TextBox2.BackColor = vbGreen
           VBAProject.UserForm1.TextBox3.BackColor = vbGreen


        If VBAProject.UserForm1.TextBox2.Text < 95 And VBAProject.UserForm1.TextBox3.Text > 3.5 Then
           VBAProject.UserForm1.BackColor = vbBlue
           VBAProject.UserForm1.TextBox2.BackColor = Gray
           VBAProject.UserForm1.TextBox3.BackColor = Gray
        Else:
           VBAProject.UserForm1.BackColor = Gray
           VBAProject.UserForm1.TextBox2.BackColor = Gray
           VBAProject.UserForm1.TextBox3.BackColor = Gray

        End If
    End If
  End With
End Sub

****EDIT****
I added the HTML for the actual table in the snippet above... The table is in the worksheet named Question1 and starts with C2 being "Student ID" and then C3 being the first row of students.

The above code compiles and runs but is not returning the Values. I added a button to open userForm1 on Sheet("Question2"), click it and the form opens. enter the number 5 for student ID (Which matches my first row student info) and it returns 0 for score and GPA when it should return the actual figures.

So, we are getting somewhere....

Array :

CEEStudent {

(0)StudentID : 5
   FinalScore : 96
   ComprehensiveTaker : 1
   StudentGPA : 3.6

(1)StudentID : 7
   FinalScore : 88
   ComprehensiveTaker : 1
   StudentGPA : 3.8

2 个答案:

答案 0 :(得分:1)

类似的事情应该起作用:

Public CEEStudent() As Student


'***** MyType as an array
Public Sub fillCEEStudent()
    Dim j As Long, i As Long

    ReDim CEEStudent(20)

    With ThisWorkbook.Sheets("Question1")
        j = 0
        For i = 3 To 22
            'might need to tweak the column positions
            CEEStudent(j).StudentID = .Cells(i, "C").Value
            CEEStudent(j).StudentGPA = .Cells(i, "H").Value
            CEEStudent(j).ComprehensiveTaker = IIf(.Cells(i, "G").Value <> "", 1, 0)
            CEEStudent(j).FinalScore = .Cells(i, "I").Value
            j = j + 1
        Next i
    End With
End Sub

答案 1 :(得分:0)

我一开始并不十分了解公立学生的知识,也不认为有必要做您需要的事情。只要您知道如何设置用户窗体(我一无所知),以下代码就可以满足您的要求。

sub macro()

Dim id as int, CEEEStudent() as variant, idRow as long, ws1 as Worksheet, ws2 as Worksheet

Set ws1 = ThisWorkbook.Worksheets("Question 1")
Set ws2 = ThisWorkbook.Worksheets("Sheet where data goes")

'insert code for a userform and store studentid = id here
id = ?


'this checks each row on the sheet and compares it to id, when it finds a match the row is stored to idRow
for x = 1 to Cells(Rows.Count, 1).End(xlUp).Row
    if id = ws1.cells(x, 1) Then
    idRow = x
    Else:
    End if
next x

'this stores the student's results to CEEEStudent(), each time a new student logs in it will overwrite the data from the previous student 
CEEEStudent(0) = ws1.Cells(idRow, 1).value
CEEEStudent(1) = ws1.Cells(idRow, 5).value
CEEEStudent(2) = ws1.Cells(idRow, 6).value
CEEEStudent(3) = ws1.Cells(idRow, 7).value

ws2.activate

'This sets the value and headers that the student will see.  Again this will be overwritten after every login.

ws2.Cells(1, 1).Value = "Student ID"
ws2.Cells(2, 1).Value = CEEEStudent(0)

ws2.Cells(1, 2).Value = "Final Exam"
ws2.Cells(2, 2).Value = CEEEStudent(1)

ws2.Cells(1, 3).Value = "GPA"
ws2.Cells(2, 3).Value = CEEEStudent(2)

ws2.Cells(1, 4).Value = "Final Score"
ws2.Cells(2, 4).Value = CEEEStudent(3)




end sub

编辑1

Public Type Student
StudentID As Integer ' Student ID
FinalScore As Integer ' Studnts final score
ComprehensiveTaker As Integer ' Comprehensivetaker
StudentGPA As Double ' Student's GPA
End Type

Public CEEStudent(20) As Student

public Sub test()

Cells(1, 1).Value = CEEStudent(20).StudentID 
Cells(1, 2).Value = CEEStudent(20).StudentGPA 
Cells(1, 3).Value = CEEStudent(20).ComprehensiveTaker 
Cells(1, 4).Value = CEEStudent(20).FinalScore                             

End sub