Reorganize data using VBA

Jimbob2000

New Member
Joined
Jun 27, 2019
Messages
25
I have a database that produces reports about tests taken by students all on one row. The data always includes a test name, test date, and one or more scores. E.g.:

TestDateAlgebraGeometryArithmeticTestDateSpelling
Math2/2/2019405060English2/5/201970

<tbody>
</tbody>

Sometimes there five or six tests, each with five or six scores.

We run these reports all the time and it would be really convenient to fix the data using a macro rather than by hand. I'm looking for a way to use VBA to convert this data into a table that looks like this:


TestDateAlgebraGeometryArithmetic
Math2/2/2019405060
TestDateSpelling
English2/5/201970

<tbody>
</tbody>

It's tricky because the number of scores for each test can vary. Any help you can provide would be very greatly appreciated!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
try this code:
Code:
Sub test()
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
inarr = Range(Cells(1, 1), Cells(2, lastcol))
indi = 4
coli = 2
' put the first "test " in
Cells(indi, 1) = inarr(1, 1)
Cells(indi + 1, 1) = inarr(2, 1)


For i = 2 To lastcol
  If inarr(1, i) = "Test" Then
   indi = indi + 2
   coli = 1
  End If
  Cells(indi, coli) = inarr(1, i)
  Cells(indi + 1, coli) = inarr(2, i)
  coli = coli + 1
Next i


End Sub
 
Upvote 0
This is works perfectly! Thank you!

As a follow up question: Sometimes, students retake sets of tests and the database creates a new row for the retakes. Some students have up to ten rows of data, each with multiple tests and multiple scores on each test. Is there any way to modify the code you created to cope with an undefined number of rows?

Essentially turning something like this:


TestDateAlgebraGeometryArithmeticTestDateSpelling
Math2/2/2019405060English2/5/201970
Math4/6/2019556575

<tbody>
</tbody>

Into an output like this:

TestDateAlgebraGeometryArithmetic
Math2/2/2019405060
TestDateSpelling
English2/5/201970
TestDateAlgebraGeometryArithmetic
Math4/6/2019556575

<tbody>
</tbody>


I really appreciate your help with this!

James
 
Upvote 0
try this code:
Code:
Sub test()
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
inarr = Range(Cells(1, 1), Cells(10, lastcol))
indi = 12
nextindi = indi
coli = 1


For i = 1 To lastcol
  If inarr(1, i) = "Test" Then
   indi = nextindi
   coli = 1
  End If


    For k = 1 To 10
    If inarr(k, i) = "" Then
    nextindi = k + indi - 1
    Exit For
    Else
    
    Cells(indi + k, coli) = inarr(k, i)
    End If
    Next k
    coli = coli + 1
Next i




End Sub
 
Upvote 0

Forum statistics

Threads
1,216,742
Messages
6,132,453
Members
449,729
Latest member
davelevnt

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