VBA to transpose data (excluding blanks) in rows based on a set of values

Gloria1111

New Member
Joined
Jul 31, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I have a spreadsheet with around 2000 rows of data and i need to transpose each row of data every n row, only that "n" is not fixed, and also to exclude the blanks.

I found a similar thread but it is only based on a fixed n row: VBA Transpose every nth row

How can i get it to transpose the row of data for Jan-June and skip the next "N" number of rows based on the indicated "N" value?

Example:

Original data
NameJanFebMarAprMayJuneN Value
John12324222
John
John
Grace23671
Grace
Jason40064623232222443655
Jason
Jason
Jason
Jason
Jason

Desired result:
NameJan
John123
John24
John22
Grace23
Grace67
Jason400
Jason646
Jason23232
Jason222
Jason443
Jason65

Any help/advice is greatly appreciated.
If my question is not clear, please let me know and i will see how i can rephrase or post a clearer example.

Thank you.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Does the "N" value always correspond to the number of blank rows for each name?
 
Upvote 0
Yes.
I managed to add in the number of blank rows according to the number of data for each person but now facing difficulty in transposing them without doing it manually.
 
Upvote 0
Start by deleting the "N" values in column H as they are not needed and then try this macro. It will place the result in "Sheet2". Change the sheet names (in red) to suit you needs.
Rich (BB code):
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim x As Variant, srcWS As Worksheet, desWS As Worksheet, v As Variant, dic As Object, r As Long, c As Long, y As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 7).Value
    ReDim x(1 To UBound(v), 1 To 1)
    Set dic = CreateObject("Scripting.Dictionary")
    For r = LBound(v) To UBound(v)
        If Not dic.Exists(v(r, 1)) Then
            dic.Add v(r, 1), Nothing
            For c = 2 To UBound(v, 2)
                If v(r, c) <> "" Then
                    y = y + 1
                    x(y, 1) = v(r, c)
                End If
            Next c
            With desWS
                If y > 0 Then
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(y) = v(r, 1)
                    .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(y) = x
                End If
            End With
        End If
    dic.RemoveAll
    y = 0
    Next r
    Application.ScreenUpdating = True
End Sub
The macro assumes that your actual data is organized in exactly the same way as the data you posted.
 
Upvote 0
Solution
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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