Solved

Transposing using VBA

Posted on 2014-02-12
5
155 Views
Last Modified: 2014-02-15
Dear Experts:

I got a one-colum data list with the following make-up in Column A

it starts with a 9 digit number with hyphens and one space
the next tow to three cells below are just text
Then comes the next 9 digit number and so forth.

What I would like to achieve using a macro is the following:

The macro is to select the first number in A1 and the two cells below (i.e. from A1 to A3) and then go to B2 and do a transposing action
The macro goes on selecting the next 9 digit number in Cell A4 and the three cells below (i.e. from A4 to A7) and then selects B4 and does a transposing action
and so forth.

I hope I could make myself clear.

Help is much appreciated. Thank you very much in advance for your valuable help.

I have attached a sample file for your convenience.

Regards, Andreas

COLUMN A
20 17-445-12
some text
more text
40 16-445-45
some text
more text
more text
30 40-442-48
some text
more text
more text
18 45-454-22
etc.
Tranposing.xlsx
0
Comment
Question by:AndreasHermle
  • 3
5 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 250 total points
Comment Utility
Try this.
Option Explicit

Sub Trans()
Dim I As Long
Dim J As Long
Dim X As Long
Dim arrSrc
Dim arrDst

    arrSrc = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value

    ReDim arrDst(1 To UBound(arrSrc), 1 To 1)

    For I = 1 To UBound(arrSrc)
        If IsNumeric(Left(arrSrc(I, 1), 2)) Then
            X = X + 1
            ReDim Preserve arrDst(1 To UBound(arrSrc), 1 To X)

            J = 1
            arrDst(J, X) = arrSrc(I, 1)
            J = J + 1
        Else
            arrDst(J, X) = arrSrc(I, 1)
            J = J + 1
        End If

    Next I
    
    Range("B1").Resize(UBound(arrDst, 2), UBound(arrDst)).Value = Application.Transpose(arrDst)
    
End Sub

Open in new window

0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 250 total points
Comment Utility
In the desired Results worksheet, you had empty rows between each "record" of data. The previously suggested macro is not providing those empty rows. The one in the snippet below gives you the choice of empty rows or not.

As written, the macro puts empty rows between records. Two statements are commented out, however. If you use them instead of their alternatives, then you will eliminate the empty rows.
Sub Transposition()
Dim vData As Variant, vResults As Variant
Dim i As Long, j As Long, k As Long, n As Long, nCols As Long, nn As Long
Dim rg As Range
nCols = 5       'Maximum number of columns of data in a a record
With ActiveSheet
    Set rg = .Range("A1")   'First cell with data
    Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)) 'All the data in that column
End With
vData = rg.Value
n = rg.Rows.Count
nn = Int((n + nCols - 1) / nCols)
ReDim vResults(1 To n, 1 To nCols)
For i = 1 To n
    If IsNumeric(Left(vData(i, 1), 2)) Then
        k = k + 1
        j = 1
    Else
        j = j + 1
    End If
    'vResults(k, j) = vData(i, 1)    'If you don't want empty rows between records
    vResults(i - j + 1, j) = vData(i, 1) 'If you want empty rows between records
Next
rg.Cells(1, 2).Resize(n, nCols).Value = vResults    'Empty rows between records
'rg.Cells(1, 2).Resize(k, nCols).Value = vResults    'No empty rows between records
End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
Comment Utility
Dear both,

both codes work just fine. I  am really deeply impressed with your excel/macro expertise. Incredible!!
I will do some more testing on my huge data list and then let you know.

Again, thank you very much

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
Comment Utility
I suggest splitting the points evenly if you do not mind
0
 

Author Closing Comment

by:AndreasHermle
Comment Utility
Thank you very, very much for your superb and swift support. I really appreciate it.

Regards, Andreas
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now