Transpose single column of data to multiple columns at blank row breaks

chzhd4life

New Member
Joined
Jan 6, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Help, have multiple very long "tables" of data in a single column. Need to transpose/convert to standard table format. Entries are divided by blank row or rows. Not all entries have same pieces of data, e.g., some have Name, Address, City/ST/Zip, Phone, email, website URL, etc. Others may be missing some pieces of information. Found a macro (below) that described my same scenario but it's not working (run-time error 1004) and when I debug it appears to be getting hung up at "Cells(strt, 1).Resize(nd - strt).Copy". Help?

Sub Paste_Transpose()
Dim LastRow As Long, x As Long
Dim PasteRow As Long
Dim strt As Long, nd As Long
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
PasteRow = 1
For x = 1 To LastRow + 1
If Len(Cells(x, 1)) = 0 And strt = 0 And nd = 0 Then
strt = Cells(x, 1).Row + 1
Else
If strt > 0 And Len(Cells(x, 1)) = 0 Then
nd = Cells(x, 1).Row
'paste transpose
Cells(strt, 1).Resize(nd - strt).Copy
Cells(PasteRow, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
strt = 0
nd = 0
x = x - 1
PasteRow = PasteRow + 1
End If
End If
Next
Application.CutCopyMode = False
'Columns(1).Delete
End Sub
 

Attachments

  • sample table.JPG
    sample table.JPG
    65.9 KB · Views: 66

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Here's an example of what it's doing in my most current example … I was thinking that because there's a blank between Prairie City OR and the phone number, it thinks it should start a NEW record with the phone number, when in fact everything up through the URL is one record and the new entry shouldn't start until after the TWO blank rows because all data up to the two blank rows is one record.

Data in left yellow column A is the original, data in columns B-H is what the macro did. Three entries starting in row with phone number belong to the record above it.

Don't get me wrong, it's AWESOME and I can happily make the occasional edits, just wondered if there was a way to easily modify the macro when I had a scenario like this. THANKS!!!
View attachment 3407

According to your example, this should work for you.

VBA Code:
Sub Paste_Transpose_1()
  Dim c As Range, d As Range, wu As Boolean, a As Variant
  Range("B:Z").ClearContents
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    Set d = c
    If wu = False Then
      If d.Cells(d.Rows.Count, 1).Offset(2) <> "" Then
        Set a = Range(d.Cells(d.Rows.Count, 1).Offset(2), Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
        Set d = Union(d.Resize(d.Rows.Count + 1, 1), a(1))
        wu = True
      End If
      Range("B" & Rows.Count).End(xlUp)(2).Resize(1, d.Rows.Count).Value = Application.Transpose(d.Value)
    Else
      wu = False
    End If
  Next
End Sub
 
Upvote 0
Darn, missed that, maxcol=7
What happens if you add in this line where shown?
Rich (BB code):
  Next i
  ReDim Preserve b(1 To UBound(b), 1 To maxcol)
  Range("B2").Resize(r, maxcol).Value = b
End Sub
 
Upvote 0
Hi @chzhd4life,

Forget the post code #12.

With the following you can even have lost several data between the lines; but not 2 consecutive blanks, because then it is considered as a new block.

Ej:
Book1
ABCDEFGHI
1
2FIELDFIELDPRODPRAI541FIELDHTTP
3PRODFINEPRODUCTSHILLSDAVIE719FINEFETTHTTP
4COMPANYPRODUC XNA 12888ABCHTTP
5PRAI
6
7541
8FIELD
9HTTP
10
11
12FINE
13PRODUCTS
14HILLS
15DAVIE
16719
17FINEFETT
18HTTP
19
20
21COMPANY
22PRODUC X
23NA 12
24
25888
26ABC
27HTTP
Hoja14



Try this:
VBA Code:
Sub Paste_Transpose_5()
  Dim c As Range
  Application.ScreenUpdating = False
  For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).Areas
    If c.Rows.Count = 1 Then c.Value = "###"
  Next
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    Range("B" & Rows.Count).End(xlUp)(2).Resize(1, c.Rows.Count).Value = Application.Transpose(c.Value)
  Next
  ActiveSheet.Cells.Replace "###", "", xlWhole, xlByRows, False, False, False
End Sub
 
Upvote 0
THANKS! That fixed the issues where I have two consecutive blanks, BUT I then found a version where I have THREE blanks, and it's still doing same, starting new entry too early. Can you tell me where in the code I might edit when I run into that or similar scenarios?

THANKS AGAIN!
 
Upvote 0
THANKS! That fixed the issues where I have two consecutive blanks, BUT I then found a version where I have THREE blanks, and it's still doing same, starting new entry too early. Can you tell me where in the code I might edit when I run into that or similar scenarios?

THANKS AGAIN!

You can put a picture of how your data is and how you want the result.
 
Upvote 0
1578497966259.png

Yellow is small section of original data, orange is first instance of where it stopped transposing one row too soon
 
Upvote 0
Yellow is small section of original data, orange is first instance of where it stopped transposing one row too soon

So the pattern should not be blank cells as you suggest in the initial post "Entries are divided by blank row or rows"
The pattern should be every 7 lines 2 spaces, then again 7 lines 2 spaces:

1578501703923.png


I guess it starts in cell A2. Then try the following:

VBA Code:
Sub Paste_Transpose_6()
  Dim c As Range, j As Long
  j = 2
  For i = 2 To Range("A" & Rows.Count).End(xlUp).Row Step 9
    Range("B" & j).Resize(1, 7).Value = Application.Transpose(Range("A" & i).Resize(7, 1).Value)
    j = j + 1
  Next
End Sub
 
Upvote 0
In this case it's 7, but not always.... Thanks for this code, I'll likely utilize all three in various scenarios! Thanks again so much!
 
Upvote 0
In this case it's 7, but not always.... Thanks for this code, I'll likely utilize all three in various scenarios! Thanks again so much!

If you find a pattern for all scenarios, I will gladly make the changes.

Nice to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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