Click here to Skip to main content
16,015,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
a	1	2	3								
b	1	2	3								
c	1	2	3			


a	1	16	17								
b	12	15	16								
c	13	14	17	



I have this kind of data in my excel .From this I want see my required output as below with help of vba script.

Required op:
a	b	c									
1	1	1									
2	2	2									
3	3	3									
1	12	13									
16	15	14									
17	16	17


Thanks In advance.

What I have tried:

VB
Option Explicit

Public Sub NormaliseData()

    Dim myRange, myDest, r As Range, i, n As Long
    
    'Change this range to encompass all your data
    Set myRange = Worksheets("Sheet5").Range("A1:D7")
    
    'Change this range to show where to start the new list
    Set myDest = Worksheets("Sheet6").Range("A1")
    
    With myRange
        For i = 1 To .Rows.Count
            For Each r In .Range(.Cells(i, 1), .Cells(i, .Columns.Count))
                If r.Column = myRange.Column Then
                    myDest.Value = r.Value
                ElseIf r.Value <> "" Then
                    myDest.Offset(0, 1).Value = r.Value
                    Set myDest = myDest.Offset(1, 0)
                Else
                    'do nothing
                End If
            Next r
        Next i
    End With

End Sub
Posted
Updated 8-Sep-16 5:08am
v3
Comments
Patrice T 8-Sep-16 3:35am    
And you plan to explain what is the problem ?

Will simply 'transposing' the data work for you?

Public Sub NormaliseData()
 
    Dim myRange, myDest, r As Range, i, n As Long
    
    'Change this range to encompass all your data
    Set myRange = Worksheets("Sheet5").Range("A1:D7")
    
    'Change this range to show where to start the new list
    Set myDest = Worksheets("Sheet6").Range("A1")
    
    myRange.Copy
    myDest.PasteSpecial Transpose:=True

End Sub
 
Share this answer
 
Comments
Member 12726897 8-Sep-16 5:01am    
By executing above code It's returning output as below.
a b c a b c
1 1 1 1 12 13
2 2 2 16 15 14
3 3 3 17 16 17

But I want the op as follows

a b c
1 1 1
2 2 2
3 3 3
1 12 13
16 15 14
17 16 17

Thanks in advance.
GoodJuJu 8-Sep-16 5:13am    
Is that your actual data, a b c... or is there a specific header that changes with each 'batch'?
Okay - try this:

Option Explicit
 
Public Sub NormaliseData()
 
    Dim rngSource, rngDest As Range
    Dim intRow As Integer
    
    Dim strSourceSheet As String
    Dim strDestSheet As String
    
    strSourceSheet = "Sheet5"
    strDestSheet = "Sheet6"
    
    Worksheets(strSourceSheet).Select
    
    For intRow = 1 To Worksheets(strSourceSheet).UsedRange.Rows.Count
        If Worksheets(strSourceSheet).Range("A" & intRow).Value Like "a" Then
            If intRow = 1 Then
                Set rngSource = Worksheets(strSourceSheet).Range("A" & intRow & ":D" & intRow + 2)
            Else
                Set rngSource = Worksheets(strSourceSheet).Range("B" & intRow & ":D" & intRow + 2)
            End If
            
            Call fncTranspose(rngSource, rngDest, intRow, strDestSheet)
            
        End If
    Next intRow
    
MsgBox "Values have been transposed", vbOKOnly, "Transposed"
End Sub

Function fncTranspose(rngSource, rngDest, intRow, strDestSheet)

Dim intLastDestRow As Integer
intLastDestRow = Worksheets(strDestSheet).UsedRange.Rows.Count

Worksheets(strDestSheet).Select

If intRow = 1 Then
    Set rngDest = Worksheets(strDestSheet).Range("A" & intLastDestRow)
    rngSource.Copy
    rngDest.PasteSpecial Transpose:=True
Else
    Set rngDest = Worksheets(strDestSheet).Range("A" & intLastDestRow + 1)
    rngSource.Copy
    rngDest.PasteSpecial Transpose:=True
End If

End Function
 
Share this answer
 
v3
Comments
Maciej Los 8-Sep-16 10:31am    
Rather than posting another solution, improve previous answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900