Flipping values in excel to column titles

Dreamteam

New Member
Joined
Feb 22, 2018
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I try to reserve any questions to this forum that are a little challenging - so apologies for the following as I really should know how to do this. In short, a taxi company has daily contracts and simply add which taxis took the job - at the end of the month the drivers get paid. The contracts are at the top, the dates on the side and whichever taxi does the job gets entered into the data in the spread sheet (taxis 3,5,6,7,8,14 or 15). All I want to do is flip this information so that the taxi references are at the top and the contracts are in the spread sheet. I have highlighted a possible scenario where one of the drivers complete more that 2 jobs in the day - I thought that this would be the only difficult part but for me I am struggling (looking at this I don't think it has come out highlighted). I have tried using Tables/Pivot Tables but to no avail.

Any help would be much appreciated.

Many thanks

Dt

CURRENT STATE

DATEDAYAM/PMCOSAWESBASS ACCSWAN MONGPL PL SAINSBERKLEY COTTPL PL DRACTREGEW
01/02/2019FRIAM5147
01/02/2019FRIPM5143
02/02/2019SATAM
02/02/2019SATPM
03/02/2019SUNAM
03/02/2019SUNPM
04/02/2019MONAM71535
04/02/2019MONPM71514
05/02/2019TUEAM8753
05/02/2019TUEPM87535

<colgroup><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>



WOULD I IMAGINE IT TO LOOK LIKE


DATEDAYAM/PM356781415
01/02/2019FRIAMCOSAWESSWAN MONGLBASS ACC
01/02/2019FRIPMSWAN MONGLCOSAWESBASS ACC
02/02/2019SATAM
02/02/2019SATPM
03/02/2019SUNAM
03/02/2019SUNPM
04/02/2019MONAMSWAN MONGLBERK COTTCOSAWESBASS ACC
04/02/2019MONPMCOSAWESSWAN MONGLBASS ACC
05/02/2019TUEAMPL PL SAINSSWAN MONGLBASS ACCCOSAWES
05/02/2019TUEPMPL PL SAINSSWAN MONGLBASS ACCCOSAWES
05/02/2019TUEPMBERK COTT

<colgroup><col><col span="2"><col span="2"><col><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:-
NB:- The word "DATE" in column "A" data is assumed to start in "A1".
NB:- This code will alter your Data !!
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Jan20
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Taxi [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Taxi = Array(3, 5, 6, 7, 8, 14, 15)
Lst = Cells("1", Columns.Count).End(xlToLeft).Column
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("D2"), Range("D" & Rows.Count).End(xlUp)).Resize(, Lst)

[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Not .exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            .Add Dn.Value, Dn
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
        
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]

[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, nRng [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Rng.ClearContents
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Taxi
        c = c + 1
        [COLOR="Navy"]If[/COLOR] .exists(Val(R)) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] .Item(Val(R))
                Cells(P.Row, c + 3) = Cells(1, P.Column).Value
            [COLOR="Navy"]Next[/COLOR] P
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] R
      Range("D1").Resize(, UBound(Taxi) + 1) = Taxi
 [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick

Many many thanks for this

Why can't I write code off the bat like this!

Your code works great - apart from when the taxi completes more than 2 jobs for the day - for example on 05/02/2019 Taxi 5 completes SWAN MONGL on both am and pm and another job in the pm.

Bit tricky I think

Many thanks

Dt
 
Last edited:
Upvote 0
Try this enhanced code for duplicates jobs in any line
Run the code from the activesheet (data sheet) for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG27Jan07
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] Range, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] oMax [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] dic [COLOR="Navy"]As[/COLOR] Object, nRng [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]With[/COLOR] ActiveSheet
    [COLOR="Navy"]Set[/COLOR] Rng = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
    Lst = .Cells("1", Columns.Count).End(xlToLeft).Column
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Set[/COLOR] dic = CreateObject("scripting.dictionary")
dic.CompareMode = vbTextCompare
oMax = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    c = c + oMax
  oMax = 0
 [COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    Dn.Resize(, Lst).Copy .Cells(c, 1)
    [COLOR="Navy"]Set[/COLOR] nRng = .Cells(c, 1).Resize(, Lst)
    dic.RemoveAll
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] nRng
        [COLOR="Navy"]If[/COLOR] R.Column > 3 [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] R.Value <> "" [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]If[/COLOR] Not dic.Exists(R.Value) [COLOR="Navy"]Then[/COLOR]
                    dic.Add R.Value, 0
                [COLOR="Navy"]Else[/COLOR]
                    dic(R.Value) = dic(R.Value) + 1
                    oMax = Application.Max(dic(R.Value), oMax)
                    nRng(1).Resize(, 3).Copy nRng(1).Offset(dic(R.Value))
                    R.Cut R.Offset(dic(R.Value))
                [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] R
oMax = oMax + 1
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Next[/COLOR] Dn
namchange
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Sub[/COLOR] namchange()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Taxi [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] dic [COLOR="Navy"]As[/COLOR] Object
Taxi = Array(3, 5, 6, 7, 8, 14, 15)
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    Lst = .Cells("1", Columns.Count).End(xlToLeft).Column
    [COLOR="Navy"]Set[/COLOR] Rng = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
    [COLOR="Navy"]Set[/COLOR] Rng = Rng.Offset(, 1).Resize(, Lst)


[COLOR="Navy"]Set[/COLOR] dic = CreateObject("scripting.dictionary")
dic.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Not dic.Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            dic.Add Dn.Value, Dn
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] dic(Dn.Value) = Union(dic(Dn.Value), Dn)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]

[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, nRng [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Rng.ClearContents
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Taxi
        c = c + 1
        [COLOR="Navy"]If[/COLOR] dic.Exists(Val(R)) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] dic(Val(R))
                .Cells(P.Row, c + 3) = .Cells(1, P.Column).Value
            [COLOR="Navy"]Next[/COLOR] P
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] R
.Range("D1").Resize(, UBound(Taxi) + 1) = Taxi
.UsedRange.Columns.AutoFit
.UsedRange.Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Mick - I don't know what to say. Well I do - 1) Thank you so much for this. I got a bit obsessed with it and I have been considering it for all of yesterday and yesterday evening. 2) How can I learn to this level of coding? I seem to spend forever on even the most basic problems.

Anyway - thank you once again :~)

Dt
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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