How to convert table from one form to other form?

SarahDetroja

New Member
Joined
May 13, 2020
Messages
18
Office Version
  1. 2013
Platform
  1. Windows
Hello Fellow Experts,

Please see attached image. I have a table in the form of "Source" format. I want to convert that table in the form of "Output" format. What is the best way to do that?

Many thanks in advance for your kind help.

Regards
Sarah.
 

Attachments

  • MrExcel.png
    MrExcel.png
    37.3 KB · Views: 20

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this with a copy of your workbook. I have assumed that your actual data starts in row 2 with headers in row 1.


VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) / 3, 1 To 5)
  For i = 1 To UBound(a) Step 3
    k = k + 1
    b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 4): b(k, 4) = a(i + 1, 4): b(k, 5) = a(i + 2, 4)
  Next i
  With Range("F2:J2").Resize(k)
    .Value = b
    .Rows(0).Value = Array("First Name", "Last Name", "Color", "Country", "City")
    .EntireColumn.AutoFit
  End With
End Sub
 
Upvote 0
Try this with a copy of your workbook. I have assumed that your actual data starts in row 2 with headers in row 1.


VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) / 3, 1 To 5)
  For i = 1 To UBound(a) Step 3
    k = k + 1
    b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 4): b(k, 4) = a(i + 1, 4): b(k, 5) = a(i + 2, 4)
  Next i
  With Range("F2:J2").Resize(k)
    .Value = b
    .Rows(0).Value = Array("First Name", "Last Name", "Color", "Country", "City")
    .EntireColumn.AutoFit
  End With
End Sub
Hi Peter,

This fantastic! That's exactly what I wanted and works like a charm.

I really appreciate it.

Thanks again.

Regards
Sarah
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0
Hi Peter,

A couple of changes which if I have coding expertise can be easily accommodated but unfortunately, I don't have.

1) Now instead of 3 questions, there are 10 questions.
2) Like "First Name" and "Last Name" there are additional two columns that would be fixed for each record. "Age" and "Join Date"

Please see attached image and link to Drive spreadsheet Sample File

Can you please provide a solution once again?

Many thanks again in advance for your kind help.

Note: If possible please tell me, in future if I have more questions and more columns (like Name, Age) what do I need to do?
 

Attachments

  • MrExcel1.png
    MrExcel1.png
    56.9 KB · Views: 10
Upvote 0
in future if I have more questions and more columns (like Name, Age) what do I need to do?
Pretty hard to give you a fixed rule as to what to do since even your fixed columns are not all at the left side etc, but see if this helps. You will have to fixe the headers section of the results yourself.

VBA Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant, FCols As Variant
  Dim i As Long, j As Long, k As Long
 
  Const NumQns As Long = 10
  Const FixedCols As String = "1 2 5 6"   'Columns A, B, E F
  Const AnswerCol As Long = 4             'Column D
  Const NumCols As Long = 6               'Total number of columns
 
 
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, NumCols).Value
  FCols = Split(FixedCols)
  ReDim b(1 To UBound(a) / NumQns, 0 To UBound(Split(FixedCols)) + NumQns)
  For i = 1 To UBound(a) Step NumQns
    k = k + 1
    For j = 0 To UBound(FCols)
      b(k, j) = a(i, FCols(j))
    Next j
    For j = UBound(FCols) + 1 To UBound(FCols) + NumQns
      b(k, j) = a(i + j - UBound(FCols) - 1, AnswerCol)
    Next j
  Next i
  With Range("Z2").Resize(k, NumQns + UBound(FCols) + 1)
    .Value = b
'    .Rows(0).Value = Array("First Name", "Last Name", .....) 'Add other headers here
    .EntireColumn.AutoFit
  End With
End Sub
 
Upvote 0
Hi Peter,

Again master class from you! Again worked like a charm!

Many thanks. It's super helpful. You made my day (&week!)

Regards
Sarah
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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