Vba to transport rows to columns- Set of data

aravindhan_31

Well-known Member
Joined
Apr 11, 2006
Messages
672
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi,

coming back after a long time:) coz i need your help very badly.

I have 2 columns like below, I simply need to to transpose these data into columns in a new sheet


List 1 List 2
Apple Boomerang
Apple Offerings
Apple Industrial Equipment
Apple Chemicals & Natural Resources
Apple Consumer Goods & Services (CGS)
Apple Air Freight Logistics&Travel Srvs(AF&TS)
Apple Life Sciences (LS)
Apple Capital Markets
Apple Utilities
Orange Insurance
Orange Banking
Orange Media & Entertainment
Orange Electronics & High Tech
Mango Communications
Mango Data Science Practice
Mango Industry Value Team
Mango Global Capability
Mango Platform Sales
Mango Data Engineering Practice


eg Col 1 with header Apple, Col 2 with header Orange, Col 3 with header Mango
and in each of those headers I need values that are there in List 2. that is under Apple i need like below

Apple
Boomerang
Offering
Industrial Equipment
Chemicals & Natural Resources
Consumer Goods & Services (CGS)
Air Freight Logistics&Travel Srvs(AF&TS)
Life Sciences (LS)
Capital Markets
Utilities

like this i need to create columns for all values from List 1, and paste data as per List 2. If i do this in pivot i get in all in one below the other.

regards
Arvind
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this for Results on sheet2.
Rich (BB code):
Sub MG23Jan21
Dim Rng As Range, Dn As Range, n As Long, K As Variant, col As Long

Set Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
    If Not .Exists(Dn.Value) Then
        .Add Dn.Value, Dn.Offset(, 1)
    Else
        Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
    End If
Next

For Each K In .keys
    col = col + 1
    Sheets("Sheet2").Cells(1, col) = K
    Sheets("Sheet2").Cells(2, col).Resize(.Item(K).Count).Value = .Item(K).Value
Next K
End With
    Sheets("Sheet2").Columns("A").Resize(, col).Columns.AutoFit
End Sub
Regards Mick
 
Last edited by a moderator:
Upvote 0
Works perfectly, thank you so much. You are a star.

quick add on, can we define Names for the Ranges? eg, A2 to A99 in sheet 2 to be "Named" as Apple, B2 to B5 to be names as "Orange" & c2 to C7 as Mango.

I tried with excel option " Create from Selection" & Name as per "Top Row", it create names for rows 2 to 9 for all , Apples, Mango & Orange, with blanks, which i dont need.

do you think you can help me:)

Regards
Arvind
 
Upvote 0
Perhaps this:-
I have created the named ranges for the length of data in each column, if you require each named range to be of other specific lengths, let me know.
Add line as shown in red
Rich (BB code):
For Each K In .keys
    col = col + 1
    Sheets("Sheet2").Cells(1, col) = K
    Sheets("Sheet2").Cells(2, col).Resize(.Item(K).Count).Value = .Item(K).Value
    Sheets("Sheet2").Cells(2, col).Resize(.Item(K).Count).Name = K
Next K
 
Last edited by a moderator:
Upvote 0
sorry for replying late, went on vacation and couldn't check this.

this one worked exactly the way i wanted.

thanks:)

Regards
Arvind
 
Upvote 0
Hi Mick

This is not working when there is a space between 2 words in column A.

Ex:

List 1List 2
Apple GrapesBoomerang
Apple GrapesOfferings
Apple GrapesIndustrial Equipment
Apple GrapesChemicals & Natural Resources
Apple GrapesConsumer Goods & Services (CGS)
Apple GrapesAir Freight Logistics&Travel Srvs(AF&TS)
Apple GrapesLife Sciences (LS)
Apple GrapesCapital Markets
Apple GrapesUtilities
Orange GrapesInsurance
Orange GrapesBanking
Orange GrapesMedia & Entertainment
Orange GrapesElectronics & High Tech
Mango GrapesCommunications
Mango GrapesData Science Practice
Mango GrapesIndustry Value Team
Mango GrapesGlobal Capability
Mango GrapesPlatform Sales
Mango GrapesData Engineering Practice
Mango GrapesData Engineering Practice
Mango GrapesData Engineering Practice
Mango GrapesData Engineering Practice
 
Upvote 0
Use
VBA Code:
    Sheets("Sheet2").Cells(2, col).Resize(.Item(K).Count).Name = Replace(K, " ", "_")
 
Upvote 0
It is not working for the cases where i have multiple spaces like this:

Chemicals & Natural Resources
Corporate Marketing & Communications
Air Freight Logistics&Travel Srvs(AF&TS)
 
Upvote 0
That's got nothing to do with the spaces, it's the fact that you have illegal characters.
The rules for names are
  • The first characterof a name must be one of the following characters:
    • letter
    • underscore (_)
    • backslash (\).
  • Remaining characters in the name can be
    • letters
    • numbers
    • periods
    • underscore characters
  • The following are not allowed:
    • Space characters are not allowed as part of a name.
    • Names can't look like cell addresses, such as A$35 or R2D2
    • C, c, R, r -- can't be used as names -- Excel uses them as selection shortcuts
from
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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