VBA code to Transpose from one column to multiple columns

peppe1985

New Member
Joined
Nov 17, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello all, I'm still fairly new to VBA and I'm at a lost how in can transpose data that I have in spreadsheet to multiple columns while deleting blank rows in between.

Attached are 2 images, the original sheet of how my data looks, the second sheet is what I'm looking to get as a final outcome.

Any help would be greatly appreciated :)

Thanks
 

Attachments

  • Original spreadsheet.PNG
    Original spreadsheet.PNG
    31.9 KB · Views: 23
  • Results.PNG
    Results.PNG
    24.7 KB · Views: 21

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Your data on sheet1, starting in cell A2. The results on sheet2.

Try:

VBA Code:
Sub TransposeColumn()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, n As Long
  Dim ar As Range
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  
  sh2.Cells.ClearContents
  i = 2
  For Each ar In sh1.Range("A2:A" & sh1.Range("F" & Rows.Count).End(3).Row).SpecialCells(xlCellTypeBlanks).Areas
    n = ar.Rows.Count
    sh2.Range("A" & i).Resize(1, 6).Value = ar.Offset(-1).Resize(1, 6).Value
    sh2.Range("G" & i).Resize(1, n).Value = Application.Transpose(ar.Offset(0, 5).Value)
    i = i + 1
  Next
End Sub
 
Upvote 0
peppe because you can never have too many solutions to a problem, here is another one. I would like to say there are many more solutions to this problem and I will continue to tweek my program. Sometimes the A plus students weigh in and get the same answer with 2 lines of code. Anyway if you have questions, comments or suggestions let us know.

VBA Code:
Option Explicit

Sub TransposeColumn()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, n As Long
  Dim LastRow As Long
  Dim Row1 As Long
  Dim Row2 As Long
  Dim Count1 As Long
  Dim Count2 As Long
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  
    sh2.Cells.ClearContents
    Row2 = 2
    Count2 = 1
 
sh1.Select
LastRow = Cells(Rows.Count, "G").End(xlUp).Row

Row1 = LastRow
Count1 = Cells(LastRow, 7)
Range("A" & LastRow - Count1, "F" & LastRow - Count1).Select
Selection.Copy
    Sheets("Sheet2").Select
    Sheets("Sheet2").Range("A2").Select
    ActiveSheet.Paste
For n = 7 To Count1 + 6
    Sheets("Sheet2").Cells(Row2, n).Select
    Sheets("Sheet2").Cells(Row2, n) = Count2
    Count2 = Count2 + 1
Next n

Count2 = 1
sh1.Select

Do Until ActiveCell.Address = sh1.Range("A2").Address

Cells(LastRow, 7).Select
Count1 = Cells(LastRow, 7)

For i = LastRow To Count1 Step -1

If Cells(LastRow, 7) = "" Then

LastRow = LastRow - 1
Count1 = Cells(LastRow, 7)

sh2.Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

Row1 = LastRow
Count1 = Cells(LastRow, 7)
Range("A" & LastRow - Count1, "F" & LastRow - Count1).Select
Selection.Copy
    Sheets("Sheet2").Select
    Sheets("Sheet2").Range("A2").Select
    ActiveSheet.Paste
For n = 7 To Count1 + 6
    Sheets("Sheet2").Cells(Row2, n).Select
    Sheets("Sheet2").Cells(Row2, n) = Count2
    Count2 = Count2 + 1
Next n

Count2 = 1
sh1.Select
Exit For

Else

Cells(LastRow, 7).Select
LastRow = LastRow - 1

End If

Next i

Loop

sh2.Select
sh2.Range("A1").Select
sh1.Select
Range("A1").Select
Application.CutCopyMode = False

End Sub



22-11-25 working 7.xlsm
ABCDEFGHIJKLMNOP
1
2NumberText0Name 111/24/2022Description12345678910
3NumberText0Name 211/24/2022Description123
4NumberText0Name 311/24/2022Description12345
5NumberText0Name 411/24/2022Description1234
6NumberText0Name 511/24/2022Description12345
Sheet2


22-11-25 working 7.xlsm
ABCDEFG
1
2NumberText0Name 111/24/2022Description
31
42
53
64
75
86
97
108
119
1210
13NumberText0Name 211/24/2022Description
141
152
163
17NumberText0Name 311/24/2022Description
181
192
203
214
225
23NumberText0Name 411/24/2022Description
241
252
263
274
28NumberText0Name 511/24/2022Description
291
302
313
324
335
Sheet1
 
Upvote 0
Perhaps I'm wrong but reading of "deleting blank rows" makes me think the two sheets reference was for sample results only and that the requirement is for a single sheet.
If that is so and in ..
- column F the descriptions are all text and the numbers are not text numbers and not the result of formulas,
- column A the cells that appear blank are in fact empty
.. then you could try this with a copy of your workbook.

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with. It will usually get you faster and better responses.

VBA Code:
Sub Rearrange()
  Dim rA As Range
  
  Application.ScreenUpdating = False
  For Each rA In Columns("F").SpecialCells(xlConstants, xlNumbers).Areas
    rA.Offset(-1, 1).Resize(1, rA.Rows.Count).Value = Application.Transpose(rA.Value)
  Next rA
  Columns("F").SpecialCells(xlConstants, xlNumbers).EntireRow.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're welcome. Glad we could help. Thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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