?
Solved

Transposing using VBA

Posted on 2014-02-12
5
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 34

Accepted Solution

by:
Norie earned 1000 total points
ID: 39853870
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 81

Assisted Solution

by:byundt
byundt earned 1000 total points
ID: 39853944
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
ID: 39854466
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
ID: 39856311
I suggest splitting the points evenly if you do not mind
0
 

Author Closing Comment

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

Regards, Andreas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

801 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