Macro to convert data to a more standard form?

REvans81

New Member
Joined
Apr 25, 2018
Messages
21
I'm not really sure how to explain this... basically I get reports on student grades for tests/projects on different dates. THe report that's generated can export to excel but it's not the easiest to work with. I'd like to make a macro to convert this data to a different format (Like student names in rows, classes in columns, scores in the cells) but the classes and size of the data set is going to vary. Ultimately, I'd like to make a pivot table with the converted data as we have a third party requesting it in this format.

sample report:

Student Name

<tbody>
</tbody>
DateClassScore TypeScore
Suzie H

<tbody>
</tbody>
1/2/2018

<tbody>
</tbody>
282 - Math 1
Quiz

<tbody>
</tbody>
90
1/8/2018

<tbody>
</tbody>
778 - Geography

<tbody>
</tbody>
Quiz

<tbody>
</tbody>
87
1/11/2018
117 - Social Studies

<tbody>
</tbody>
Homework

<tbody>
</tbody>
89
2/4/2018

<tbody>
</tbody>
282 - Math 1
Homework

<tbody>
</tbody>
89
2/7/2018

<tbody>
</tbody>
282 - Math 1
Homework

<tbody>
</tbody>
95
2/12/2018

<tbody>
</tbody>
778 - Geography

<tbody>
</tbody>
Quiz

<tbody>
</tbody>
77
Suzie HSponsor: Jim KAverage87.83
Jimmy R
1/2/2018

<tbody>
</tbody>
286 - Math 2

<tbody>
</tbody>
Homework85
1/8/2018
134 - Earth Science

<tbody>
</tbody>
Quiz89
1/9/2018

<tbody>
</tbody>
117 - Social Studies

<tbody>
</tbody>
Homework84
1/10/2018

<tbody>
</tbody>
286 - Math 2Homework90
2/2/2018

<tbody>
</tbody>
778 - Geography

<tbody>
</tbody>
Homework89
2/8/2018

<tbody>
</tbody>
778 - Geography

<tbody>
</tbody>
Project85
4/5/2018

<tbody>
</tbody>
286 - Math 2Homework77
Jimmy RSponsor: Jim KAverage85.57

<tbody>
</tbody>


The only items I really care about are the names, dates, class, and score (I don't need sponsor, average, or score type). I don't care what it looks like when it's converted, I just have to be able make a pivot table with the data.

I haven't had any formal training and I'm not very good at starting visual basic projects so I don't really know how to kick it off but nobody else here knows much of anything about excel. I know how to make pivot tables and I can usually tinker with the code after it's written in order to tweak it a bit.

If anyone is able to assist, I'd appreciate it!!!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi & welcome to the board.
How about
Code:
Sub rearrangeData()

   Dim Cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim Rng As Range
   Dim Ary() As Long
   Dim i As Long
   
   i = 1
   Set Ws1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
   Set Ws2 = Sheets.Add(, Sheets(Sheets.Count))
   Ws2.name = "[COLOR=#ff0000]Sheet2[/COLOR]"
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws1.Range("D2", Ws1.Range("D" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) And Not IsEmpty(Cl.Value) Then
            .Add Cl.Value, i
            i = i + 1
         End If
      Next Cl
      Ws2.Range("A1").Value = "Student Name"
      Ws2.Range("B1").Resize(, .Count).Value = .keys
      For Each Rng In Ws1.Range("D2", Ws1.Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
         ReDim Ary(1 To .Count)
         For Each Cl In Rng
            If .exists(Cl.Value) Then Ary(.Item(Cl.Value)) = Ary(.Item(Cl.Value)) + Cl.Offset(, 2).Value
         Next Cl
         With Ws2.Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Rng.Offset(, -3).Resize(1, 1).Value
            .Offset(, 1).Resize(, UBound(Ary)).Value = Ary
         End With
      Next Rng
   End With
End Sub
Change sheet names in red to suit
 
Upvote 0
Thank you for your reply and apologies for the duplicated post.

I'm getting a type mismatch error at

If .exists(Cl.Value) Then Ary(.Item(Cl.Value)) = Ary(.Item(Cl.Value)) + Cl.Offset(, 2).Value


and my output is (with the error)


Student Name Class 90 87 89 89 95 77


Could it be because some class names have special characters? (Like '741 - [AP] Math')
 
Upvote 0
Ahh, I had it in row 2.

Now I don't get errors but it doesn't bring in the class names still. Printing like this:

Student Name score score score score score score score score score score score score
Suzie H score score score score score score score score score score score score

What I'm looking for is for it to list one of each class (Each student may have different classes) and then sum of all scores for a student per class. Definitely moving in the right direction
 
Upvote 0
With data like


Excel 2013 32 bit
ABCDEF
1Student NameDateClassScore TypeScore
2Suzie H01/02/2018282 - Math 1Quiz90
301/08/2018778 - GeographyQuiz87
401/11/2018117 - Social StudiesHomework89
502/04/2018282 - Math 1Homework89
602/07/2018282 - Math 1Homework95
702/12/2018778 - GeographyQuiz77
8Suzie HSponsor: Jim KAverage87.83
9
10Jimmy R01/02/2018286 - Math 2Homework85
1101/08/2018134 - Earth ScienceQuiz89
1201/09/2018117 - Social StudiesHomework84
1301/10/2018286 - Math 2Homework90
1402/02/2018778 - GeographyHomework89
1502/08/2018778 - GeographyProject85
1604/05/2018286 - Math 2Homework77
17Jimmy RSponsor: Jim KAverage85.57
Sheet1


I end up with


Excel 2013 32 bit
ABCDEF
1Student Name282 - Math 1778 - Geography117 - Social Studies286 - Math 2134 - Earth Science
2Suzie H2741648900
3Jimmy R01748425289
Sheet2


Does your data look anything like mine?
 
Upvote 0
I see what I did now, this works perfectly! THANK YOU!

Now I just need to duplicate it and modify it in a way to handle a similar but different report. Basically the same output,

aEdxo4d.png



Here's the report

rKpr6h3.png




I am immensely grateful for the time you put in here!
 
Last edited:
Upvote 0
Glad to help & thanks for the feedback.

If you need help modifying it, just let me know
 
Upvote 0
IF you have extra time and feel up to it, I'd love to see what it should look like for comparison (I have several other reports after this but this one will be the last I ask about). Please don't make this a priority though, you've already helped me quite a bit and I appreciate it.
 
Upvote 0
Tell you what.
You modify the code for your other report & post it here, then I'll have a look & suggest any changes.
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,856
Members
449,411
Latest member
adunn_23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top