Home

Need to restructure my data set. Any macro for this?

%3CLINGO-SUB%20id%3D%22lingo-sub-689104%22%20slang%3D%22en-US%22%3ENeed%20to%20restructure%20my%20data%20set.%20Any%20macro%20for%20this%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-689104%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20working%20with%20a%20large%20data%20set%20that%20outputs%20reports%20grouped%20by%20date%2C%20with%20territory%20values%20comma%20delimited%20in%20a%20single%20cell%2C%20as%20follows%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BA%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20B%3C%2FP%3E%3CP%3E1%26nbsp%3B%20Date1%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BTerritory1%2CTerritory2%2CTerritory3%3C%2FP%3E%3CP%3E2%26nbsp%3B%20Date2%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BTerritory4%3C%2FP%3E%3CP%3E3%26nbsp%3B%20Date3%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BTerritory5%2CTerritory6%3C%2FP%3E%3CP%3EEtc..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20do%20analysis%20by%20territory%20and%20I'm%20looking%20for%20a%20way%20to%20produce%20an%20output%20similar%20to%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BA%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BB%3C%2FP%3E%3CP%3E1%20Territory1%26nbsp%3B%20%26nbsp%3BDate1%3C%2FP%3E%3CP%3E2%20Territory2%26nbsp%3B%20%26nbsp%3BDate1%3C%2FP%3E%3CP%3E3%20Territory3%26nbsp%3B%20%26nbsp%3BDate1%3C%2FP%3E%3CP%3E4%20Territory4%26nbsp%3B%20%26nbsp%3BDate2%3C%2FP%3E%3CP%3E5%20Territory5%26nbsp%3B%20%26nbsp%3BDate3%3C%2FP%3E%3CP%3E6%20Territory6%26nbsp%3B%20%26nbsp%3BDate3%3C%2FP%3E%3CP%3EEtc...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20discovered%20the%20'Text%20to%20Columns'%20feature%20and%20am%20able%20to%20split%20the%20territory%20values%20into%20separate%20columns%2C%20but%20so%20far%20I%20can't%20figure%20out%20how%20to%20merge%20them%20into%20a%20singular%20column%20connected%20to%20the%20appropriate%20date.%20I've%20spent%20all%20day%20looking%20up%20macros%20and%20messing%20around%20with%20the%20Power%20Query%20Editor%20to%20no%20avail.%20I'm%20about%20ready%20to%20give%20up%20and%20start%20doing%20all%20of%20this%20manually.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20pretty%20obviously%20out%20of%20my%20depth%20here.%20Any%20suggestions%20on%20how%20to%20accomplish%20this%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EPatrick%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-689104%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-689115%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20to%20restructure%20my%20data%20set.%20Any%20macro%20for%20this%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-689115%22%20slang%3D%22en-US%22%3E%3CP%3Eyou%20can%20do%20it%20manually.%3C%2FP%3E%3CP%3Eafter%20separating%20the%20territories%20into%20different%20columns%2C%20you%20need%20to%20copy%20the%20date%20column%20in%20between%20all%20the%20columns.%26nbsp%3B%20Then%20you%20just%20need%20to%20copy%20the%202nd%20set%20of%26nbsp%3B%20DATE%2FTERRITORY%20below%20the%201st%20set%2C%20then%20the%203rd%20set%2C%20etc.%3C%2FP%3E%3CP%3Ethen%20you%20sort%20all%20the%20sets%20by%20territory%20and%20date.%3C%2FP%3E%3CP%3Eyou%20probably%20will%20end%20with%20many%20dates%20without%20a%20territory.%20Those%20rows%20you%20can%20just%20delete.%3C%2FP%3E%3CP%3Enow%20you%20just%20have%20to%20make%20your%20territory%20column%20your%20first%20one.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-689140%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20to%20restructure%20my%20data%20set.%20Any%20macro%20for%20this%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-689140%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F359342%22%20target%3D%22_blank%22%3E%40pbmc81%3C%2FA%3Ein%20case%20you%20have%20too%20many%20territories%20for%20each%20date%2C%20you%20can%20run%20this%20macro.%26nbsp%3B%20Just%20select%20the%20first%20date%20of%20your%20data%20and%20run%20the%20macro%3C%2FP%3E%3CP%3ESub%20PREPARE_DATA()%3CBR%20%2F%3E'%20------------------------%3CBR%20%2F%3E'SELECT%20FIRST%20DATE%3CBR%20%2F%3EXADDR%20%3D%20ActiveCell.Address%3CBR%20%2F%3EXROW1%20%3D%20ActiveCell.Row%3CBR%20%2F%3EIf%20Len(Cells(ActiveCell.Row%20%2B%201%2C%20ActiveCell.Column))%20%26lt%3B%26gt%3B%200%20Then%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20Selection.End(xlDown).Select%20'%20MOVE%20TO%20BOTTOM%20OF%20THE%20LIST%20IF%20MORE%20THAN%201%20ROW%20OF%20DATA%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EXROW2%20%3D%20ActiveCell.Row%3CBR%20%2F%3EXROWS%20%3D%20XROW2%20-%20XROW1%20%2B%201%3CBR%20%2F%3EXCOL%20%3D%20ActiveCell.Column%3CBR%20%2F%3ERange(XADDR).Select%3C%2FP%3E%3CP%3EXNEWROW%20%3D%20XROW2%20%2B%204%20'%20row%20where%20your%20data%20will%20go%3C%2FP%3E%3CP%3EFor%20I%20%3D%20XROW1%20To%20XROW2%3CBR%20%2F%3E%26nbsp%3B%20XDATE%20%3D%20ActiveCell.Value%3CBR%20%2F%3E%26nbsp%3B%20Selection.Offset(0%2C%201).Select%3CBR%20%2F%3E%26nbsp%3B%20While%20Len(ActiveCell.Value)%20%26lt%3B%26gt%3B%200%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20XTERRITORY%20%3D%20ActiveCell.Value%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Cells(XNEWROW%2C%20XCOL)%20%3D%20XDATE%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Cells(XNEWROW%2C%20XCOL%20%2B%201)%20%3D%20XTERRITORY%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20XNEWROW%20%3D%20XNEWROW%20%2B%201%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Selection.Offset(0%2C%201).Select%3CBR%20%2F%3E%26nbsp%3B%20Wend%3CBR%20%2F%3E%26nbsp%3B%20Cells(ActiveCell.Row%20%2B%201%2C%20XCOL).Select%3CBR%20%2F%3ENext%20I%3C%2FP%3E%3CP%3ECells(XROW2%20%2B%204%2C%20XCOL).Select%3CBR%20%2F%3E'%20------------------------%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-689803%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20to%20restructure%20my%20data%20set.%20Any%20macro%20for%20this%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-689803%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F359342%22%20target%3D%22_blank%22%3E%40pbmc81%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHi%20Patrick%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20way%20is%20to%20transform%20by%20Power%20Query%20-%20convert%20strings%20with%20Territory%20to%20list%20and%20expand%20it%20into%20new%20rows%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20406px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F118018i9D2A330D233C9257%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
pbmc81
Occasional Visitor

Hello,

 

I'm working with a large data set that outputs reports grouped by date, with territory values comma delimited in a single cell, as follows:

 

       A              B

1  Date1     Territory1,Territory2,Territory3

2  Date2     Territory4

3  Date3     Territory5,Territory6

Etc..

 

I need to do analysis by territory and I'm looking for a way to produce an output similar to below:

 

       A               B

1 Territory1   Date1

2 Territory2   Date1

3 Territory3   Date1

4 Territory4   Date2

5 Territory5   Date3

6 Territory6   Date3

Etc...

 

I've discovered the 'Text to Columns' feature and am able to split the territory values into separate columns, but so far I can't figure out how to merge them into a singular column connected to the appropriate date. I've spent all day looking up macros and messing around with the Power Query Editor to no avail. I'm about ready to give up and start doing all of this manually.

 

I'm pretty obviously out of my depth here. Any suggestions on how to accomplish this would be greatly appreciated.

 

Thanks,

Patrick

3 Replies

you can do it manually.

after separating the territories into different columns, you need to copy the date column in between all the columns.  Then you just need to copy the 2nd set of  DATE/TERRITORY below the 1st set, then the 3rd set, etc.

then you sort all the sets by territory and date.

you probably will end with many dates without a territory. Those rows you can just delete.

now you just have to make your territory column your first one.

@pbmc81in case you have too many territories for each date, you can run this macro.  Just select the first date of your data and run the macro

Sub PREPARE_DATA()
' ------------------------
'SELECT FIRST DATE
XADDR = ActiveCell.Address
XROW1 = ActiveCell.Row
If Len(Cells(ActiveCell.Row + 1, ActiveCell.Column)) <> 0 Then
   Selection.End(xlDown).Select ' MOVE TO BOTTOM OF THE LIST IF MORE THAN 1 ROW OF DATA
End If
XROW2 = ActiveCell.Row
XROWS = XROW2 - XROW1 + 1
XCOL = ActiveCell.Column
Range(XADDR).Select

XNEWROW = XROW2 + 4 ' row where your data will go

For I = XROW1 To XROW2
  XDATE = ActiveCell.Value
  Selection.Offset(0, 1).Select
  While Len(ActiveCell.Value) <> 0
    XTERRITORY = ActiveCell.Value
    Cells(XNEWROW, XCOL) = XDATE
    Cells(XNEWROW, XCOL + 1) = XTERRITORY
    XNEWROW = XNEWROW + 1
    Selection.Offset(0, 1).Select
  Wend
  Cells(ActiveCell.Row + 1, XCOL).Select
Next I

Cells(XROW2 + 4, XCOL).Select
' ------------------------
End Sub

@pbmc81 

 

Hi Patrick,

 

Another way is to transform by Power Query - convert strings with Territory to list and expand it into new rows

image.png

 

Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
38 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies