VBA: copy and paste in horizontal

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm struggling with a database in which some data (in the example, names of cities) could be repeated a lot of times.

I need to report in another sheet the data, and then report in horizontal the related datas.

See what I mean in the image https://imgur.com/a/HMMWHae


I have succeed in copy and paste the first column without duplicates. Now I need some hints to complete the job, atleast in what sort of approach is needed.

Thank's.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about
Code:
Sub Copytrans()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Dic(Cl.Value) & Cl.Offset(, 1).Value & "|" & Cl.Offset(, 2).Value & "|"
   Next Cl
   
   For Each Ky In Dic.keys
      With Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp)
         .Offset(1).Value = Ky
         .Offset(1, 1).Resize(, UBound(Split(Dic(Ky), "|")) + 1).Value = Split(Dic(Ky), "|")
      End With
   Next Ky
End Sub
 
Upvote 0
How about
Code:
Sub Copytrans()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Dic(Cl.Value) & Cl.Offset(, 1).Value & "|" & Cl.Offset(, 2).Value & "|"
   Next Cl
   
   For Each Ky In Dic.keys
      With Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp)
         .Offset(1).Value = Ky
         .Offset(1, 1).Resize(, UBound(Split(Dic(Ky), "|")) + 1).Value = Split(Dic(Ky), "|")
      End With
   Next Ky
End Sub

It works fine. Thank you.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
How about
Code:
Sub Copytrans()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Dic(Cl.Value) & Cl.Offset(, 1).Value & "|" & Cl.Offset(, 2).Value & "|"
   Next Cl
   
   For Each Ky In Dic.keys
      With Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp)
         .Offset(1).Value = Ky
         .Offset(1, 1).Resize(, UBound(Split(Dic(Ky), "|")) + 1).Value = Split(Dic(Ky), "|")
      End With
   Next Ky
End Sub

Hi @Fluff, I've adapted correctly the code for my job, except for one aspect: on the receiving sheet I need column B and column C empty, beginning to report in horizontal from column D (see image).

https://imgur.com/a/GJ8fZb2
 
Last edited:
Upvote 0
Simply change this
Code:
With Sheets("Sheet2").Range("[COLOR=#ff0000]D[/COLOR]" & Rows.Count).End(xlUp)
 
Upvote 0
Simply change this
Code:
With Sheets("Sheet2").Range("[COLOR=#ff0000]D[/COLOR]" & Rows.Count).End(xlUp)

Not exaclty: column A has to remain as before, column B and C empty, then from column D the other data.
 
Upvote 0
In that case change this
Code:
.Offset(1, 3).Resize
 
Upvote 0
In that case change this
Code:
.Offset(1, 3).Resize

Yes, that's it.

But what about the cells format?
After moving, they all seem to became "general", but some are in a different format.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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