vba loop to copy data and paste it on another worksheet

lhynlen

New Member
Joined
Jul 6, 2015
Messages
35
Hi! i need your help in copying the data in Sheet1 and pasting it in Sheet2

I have this data in Sheet1.

The number of rows is unknown but the pattern is the same.

CoumnAColumnB
Name
fruit1pears
fruit2apple
fruit3strawberry
fruit4grapes
fruit5melon
fruit6banana
Name
fruit1apple
fruit2melon
fruit3banana
fruit4strawberry
fruit5apple
fruit6grapes
Name
fruit1banana
fruit2apple
fruit3peaches
fruit4grapes
fruit5watermelon
fruit6apple

<tbody>
</tbody>



Basically i want a loop that can copy the data vertically and then pasting it in Sheet2 horizontally.

This is my desired result in Sheet2.

etc1etc2fruit1fruit2fruit33fruit4fruit5fruit6
pearsapplestrawberrygrapesmelonbanana
applemelonbananastrawberryapplegrapes
bananaapplepeachesgrapeswatermelonapple

<tbody>
</tbody>


Thanks in advance! :)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
[color=darkblue]Sub[/color] Transpoase_Fruit()
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]With[/color] Sheets("Sheet1")
        [color=darkblue]For[/color] i = 2 [color=darkblue]To[/color] .Range("B" & Rows.Count).End(xlUp).Row [color=darkblue]Step[/color] 7
            Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(, 6).Value = _
                Application.Transpose(.Range("B" & i).Resize(6).Value)
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
I just got the last last row minus the apple in Sheet2

bananaapplepeachesgrapeswatermelon

<tbody>
</tbody>
 
Last edited:
Upvote 0
It don't see where etc. etc. columns come from. I guess one might be Name. It looks like you want more of a database sort of transposition rather than a simple transposition. Once you explain my confusion, a solution can be shown that meets your goal.
 
Upvote 0
just ignore the etc columns in sheet 2, i just added them because i want to start pasting the values in column C/3rd column.
 
Upvote 0
Not the most efficient but maybe:
Code:
Sub Collate2Cols()
  Dim r As Range, c As Range, a() As Variant, i As Long, j As Long
  
  Set r = Worksheets("Sheet1").Range("A1", _
    Worksheets("Sheet1").Range("A1").End(xlDown))
  a() = UniqueValues(r)
  
  With Worksheets("Sheet2")
    .Range("A1").Resize(, UBound(a)).Value = a()
    For i = 1 To UBound(a)
      j = 1
      For Each c In r
       If a(i) = c.Value Then
          j = j + 1
          .Cells(j, i).Value = c.Offset(, 1).Value
        End If
      Next c
    Next i
  End With
End Sub

'http://msdn.microsoft.com/en-us/library/aa730921.aspx
'http://www.mrexcel.com/forum/showthread.php?t=329212
Function UniqueValues(theRange As Range) As Variant
    Dim colUniques As New VBA.Collection
    Dim vArr As Variant
    Dim vCell As Variant
    Dim vLcell As Variant
    Dim oRng As Excel.Range
    Dim i As Long
    Dim vUnique As Variant
    Set oRng = Intersect(theRange, theRange.Parent.UsedRange)
    vArr = oRng
    On Error Resume Next
    For Each vCell In vArr
    If vCell <> vLcell Then
        If Len(CStr(vCell)) > 0 Then
             colUniques.Add vCell, CStr(vCell)
        End If
    End If
    vLcell = vCell
    Next vCell
    On Error GoTo 0
 
    ReDim vUnique(1 To colUniques.Count)
    For i = LBound(vUnique) To UBound(vUnique)
      vUnique(i) = colUniques(i)
    Next i
 
    UniqueValues = vUnique
End Function
 
Upvote 0
THIS PROBLEM IS RESOLVED! Both of them works :) I just adjusted the HEADER PART and works like a CHARM!!! Thank you for all your help :)
 
Upvote 0

Forum statistics

Threads
1,217,981
Messages
6,139,763
Members
450,230
Latest member
RStasicky

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