Transpose single row into columns based on value

EbonyG

New Member
Joined
Jan 30, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello all,
I'm having trouble with data. I have a large output that I initially used a Macro to transpose every n rows to a column. Long story short, that didn't work for the output. So I'm trying to find a way to transpose a single row into columns based on the a common value. This is an example of what I have:
1580430171013.png


What I would like to do is transpose to a new row every time the text contains "Number:" for something like this:
1580430364926.png



Any help would be greatly appeciated
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this
Assuming the data starts in A1

VBA Code:
Sub TransposeData()
  Dim a As Variant, b As Variant, i As Long, j As Long, k As Long
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 100)
  For i = 1 To UBound(a)
    If UCase(Left(a(i, 1), 6)) = UCase("Number") Then
      k = 1
      j = j + 1
    End If
    b(j, k) = a(i, 1)
    k = k + 1
  Next
  Range("C1").Resize(j, 100).Value = b
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
When I tried this after transferring data from my notepad, I got subscript error 9 out of range with b(j, k) = a(i, 1) on the debug
 
Upvote 0
I got subscript error 9 out of range with b(j, k) = a(i, 1) on the debug

That is because cell A1 does not start with "Number".
Try now:

VBA Code:
Sub TransposeData()
  Dim a As Variant, b As Variant, i As Long, j As Long, k As Long
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 100)
  For i = 1 To UBound(a)
    If UCase(Left(a(i, 1), 6)) = UCase("Number") Then
      k = 1
      j = j + 1
    End If
    If k > 0 Then
      b(j, k) = a(i, 1)
      k = k + 1
    End If
  Next
  Range("C1").Resize(j, 100).Value = b
End Sub
 
Upvote 0
Another option if there is no space in the "Number:" line
VBA Code:
Sub EbonyG()
   Dim Ar As Areas
   Dim Rng As Range
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Replace "number:", "=xxxnumber", xlPart, , False, , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=xxxnumber", "Number:", xlPart, , False, , False, False
   End With
   For Each Rng In Ar
      Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(, Rng.Count + 1).Value = Application.Transpose(Rng.Offset(-1).Resize(Rng.Count + 1))
   Next Rng
End Sub
 
Upvote 0
Got it. I had to use text to columns to justify the “number” rows left. Worked using the original script
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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