VBA for Combing all data into a single column

Alex0013

Board Regular
Joined
Jul 23, 2014
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Hi All,

This may already be answered on here, but couldn't find exactly what I was looking for...

I want to take this, data pasted into Excel as rows, with varying lengths of columns (can assume I paste into Column B):

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD]g[/TD]
[TD]h[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]i[/TD]
[TD]j[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]k[/TD]
[TD]l[/TD]
[TD]m[/TD]
[TD]n[/TD]
[TD]o[/TD]
[TD]p[/TD]
[TD]q[/TD]
[TD]r[/TD]
[TD]s[/TD]
[TD]t[/TD]
[/TR]
[TR]
[TD]u[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]v[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]w[/TD]
[TD]x[/TD]
[TD]y[/TD]
[TD]z[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]












And convert to this, a single column with all the data and no blank rows (Can assuming I want this in the same sheet in Column A):
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]a[/TD]
[/TR]
[TR]
[TD]b[/TD]
[/TR]
[TR]
[TD]c[/TD]
[/TR]
[TR]
[TD]d[/TD]
[/TR]
[TR]
[TD]e[/TD]
[/TR]
[TR]
[TD]f[/TD]
[/TR]
[TR]
[TD]g[/TD]
[/TR]
[TR]
[TD]h[/TD]
[/TR]
[TR]
[TD]i[/TD]
[/TR]
[TR]
[TD]j[/TD]
[/TR]
[TR]
[TD]k[/TD]
[/TR]
[TR]
[TD]l[/TD]
[/TR]
[TR]
[TD]m[/TD]
[/TR]
[TR]
[TD]n[/TD]
[/TR]
[TR]
[TD]o[/TD]
[/TR]
[TR]
[TD]p[/TD]
[/TR]
[TR]
[TD]q[/TD]
[/TR]
[TR]
[TD]r[/TD]
[/TR]
[TR]
[TD]s[/TD]
[/TR]
[TR]
[TD]t[/TD]
[/TR]
[TR]
[TD]u[/TD]
[/TR]
[TR]
[TD]v[/TD]
[/TR]
[TR]
[TD]w[/TD]
[/TR]
[TR]
[TD]x[/TD]
[/TR]
[TR]
[TD]y[/TD]
[/TR]
[TR]
[TD]z[/TD]
[/TR]
</tbody>[/TABLE]


Any help on this would be amazing!
Thanks!

Excel 2016 64-bit
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
Code:
Sub TransposeAll()
   Dim Iary As Variant, Oary As Variant
   Dim r As Long, c As Long, i As Long
   
   Iary = Range("B1").CurrentRegion.value2
   ReDim Oary(1 To UBound(Iary) * UBound(Iary, 2), 1 To 2)
   
   For r = 1 To UBound(Iary)
      For c = 1 To UBound(Iary, 2)
         If Iary(r, c) <> "" Then
            i = i + 1
            Oary(i, 1) = Iary(r, c)
         End If
      Next c
   Next r
   Range("A1").Resize(i).Value = Application.Index(Oary, 0, 1)
End Sub
 
Upvote 0
I have no clue what Fluff has written and I bow to his/her expertise.

However I've written this so I'll post it

Code:
Sub OneCol()
    Dim c As Range
    Dim col As New Collection
    
    'get all cell values into a collection
    For Each c In Me.UsedRange
        If c <> "" Then col.Add (c)
    Next c
    
    'clear the sheet
    Me.UsedRange.Clear
    
    'write all values back to col A
    For i = 1 To col.Count
        Range("A" & i) = col(i)
    Next i
    
End Sub
 
Upvote 0
another method
Code:
Sub t()
Dim c As Range
With ActiveSheet
    Columns(1).Insert
        For Each c In .Range("B2", .Cells(Rows.Count, 2).End(xlUp))
            If c <> "" Then
                .Range(c, .Cells(c.Row, Columns.Count).End(xlToLeft)).Copy
                .Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues, Transpose:=True
            End If
        Next
        .UsedRange.Offset(, 1).ClearContents
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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