Question:
I'm using the following VBA code to store simulation results generated by R programming software in a tabular form in specified columns. However, the code seems to skip some results while placing them in the column I've specified for storing the results. I've tried multiple approaches, but I'm unable to achieve the desired outcome.
Sub ExtractStudentMarks()
' Define variables
Dim studentName As String
Dim studentMarks As Range
Dim examNumber As Integer
' Start from row 2 to skip headers
For i = 2 To 100
' Check if student name is one of the four
studentName = Range("B" & i).Value
Select Case studentName
Case "MLE"
' Calculate exam number (integer division by 4, rounded up)
examNumber = Int((i - 1) / 3) + 1
' Write student name to column I
Range("I" & examNumber).Value = studentName
' Write marks to columns J, K, and L
Range("J" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_se_ES(MSE)"
' Calculate exam number (integer division by 4, rounded up)
examNumber = Int((i - 2) / 3) + 1
' Write student name to column I
Range("I" & examNumber).Value = studentName
' Write marks to columns J, K, and L
Range("J" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_bse_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 3) / 3) + 1 ' Adjust offset for Cq and Dq
' Write student name to column I
Range("I" & examNumber).Value = studentName
' Write marks to columns J, K, and L
Range("J" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_ln_m_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 1) / 4) + 1
' Write student name to column O
Range("P" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("Q" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_ge_m_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 2) / 4) + 1 ' Adjust offset for Cq and Dq
' Write student name to column O
Range("P" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("Q" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_bln_m_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 3) / 4) + 1
' Write student name to column O
Range("P" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("Q" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_bge_m_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 4) / 4) + 1 ' Adjust offset for Cq and Dq
' Write student name to column O
Range("P" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("Q" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_ln_p_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 1) / 4) + 1
' Write student name to column O
Range("V" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("W" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_ge_p_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 2) / 4) + 1 ' Adjust offset for Cq and Dq
' Write student name to column O
Range("V" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("W" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_bln_p_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 3) / 4) + 1
' Write student name to column O
Range("V" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("W" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case "mh_bge_p_ES(MSE)"
' Calculate exam number
examNumber = Int((i - 4) / 4) + 1 ' Adjust offset for Cq and Dq
' Write student name to column O
Range("V" & examNumber).Value = studentName
' Write marks to columns P, Q, and R
Range("W" & examNumber).Resize(1, 4).Value = Range("C" & i & ":F" & i).Value
Case Else
' Handle unexpected student names (optional)
Debug.Print "Unknown student name:" & studentName
End Select
Next i
End Sub
The code is supposed to write the student name to specific columns based on prefixes and store their marks in adjacent columns. However, it appears to miss some results.
Can someone please review my code and provide guidance on why it might be skipping results and how to correct it?
This Image shows original data
(I'm new to VBA)