Code To Move From Vertical To Horizontal?

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have sheet 1 as laid out below. Column A will have a list of numbers the same (then they change) with different numbers next to them. I need them to be put on sheet 2 with the numbers in column B next to them with a slash and a gap added, rather than in a list, like the result in sheet 2 and the same when the number in A changes and so on.... Thanks

Before Code

Excel 2010
AB
8MS00146531222
9MS00160811067
10MS00160814507
11MS001500309838
12MS0019609992380
13MS001377 906 309C
14MS00260811534
15MS00216137039
16MS0025234313
17MS00233000153

<tbody>
</tbody>
Sheet1



After Code

Excel 2010
AB
2MS00146531222/ 60811067/ 60814507/ 500309838/ 9609992380/ 377 906 309C
3MS00260811534/ 16137039/ 5234313/ 33000153

<tbody>
</tbody>
Sheet2
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Dazzawm

I've got a UDF I sue for this kind of scenario. It should help you out.

Code:
Option Explicit
Option Compare Text

Public Function GROUPON(group_key As Variant, table_array As Range, col_index_num As Long, Optional delim As String = " ")

    Application.Volatile

    Dim s               As String
    Dim cell            As Range
    Dim rangeToCheck    As Range
    
    With table_array
        Set rangeToCheck = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1))
    End With
    
    For Each cell In rangeToCheck
        If cell.Value = group_key Then s = s & cell.Offset(0, col_index_num - 1).Value & delim
    Next cell
    
    If Len(s) > 0 Then s = Left(s, Len(s) - Len(delim))
    GROUPON = s

End Function

Public Sub DescribeFunction_GROUPON()
    Dim FuncName As String
    Dim FuncDesc As String
    Dim Category As String
    Dim ArgDesc(1 To 5) As String
    
    FuncName = "GROUPON"
    FuncDesc = "Concatenates values from a table based on a provided key in the first column"
    Category = 7 'Text category
    ArgDesc(1) = "is a unique key upon which to group values"
    ArgDesc(2) = "is the Range in which the values are to be found where group_key will be found in the first column"
    ArgDesc(3) = "is the column number in table_array which contains the values to return (where the first column is 1)"
    ArgDesc(4) = "is the symbol to use as a delimiter between the values, where the default is a single space"
       
    Application.MacroOptions _
        Macro:=FuncName, _
        Description:=FuncDesc, _
        Category:=Category, _
        ArgumentDescriptions:=ArgDesc
        
End Sub

It's used as follows...

Excel 2010
AB
1AB
2MS00146531222
3MS00160811067
4MS00160814507
5MS001500309838
6MS0019609992380
7MS001377 906 309C
8MS00260811534
9MS00216137039
10MS0025234313
11MS00233000153
12
13MS00146531222/ 60811067/ 60814507/ 500309838/ 9609992380/ 377 906 309C
14MS00260811534/ 16137039/ 5234313/ 33000153

<tbody>
</tbody>
Sheet1
Worksheet Formulas
CellFormula
B13=GROUPON(A13, $A$1:$B$11, 2, "/ ")
B14=GROUPON(A14, $A$1:$B$11, 2, "/ ")

<tbody>
</tbody>

<tbody>
</tbody>

Hope that helps
/AJ
 
Upvote 0
Copy the code in to a module in your workbook: ALT+F11 to access the editor then Insert --> Module. Save it as a Macro-Enabled workbook.

Now you can use the function =GROUPON in your worksheet as shown.
Code:
=GROUPON(group_key, table_array, col_index_num, [delim])

Where...
Code:
group_key is a unique key upon which to group values
table_array is the Range in which the values are to be found where group_key will be found in the first column
col_index_num is the column number in table_array which contains the values to return (where the first column is 1)
[delim] is an optional argument for symbol to use as a delimiter between the values, where the default is a single space
(It works syntactically very similiarly to VLOOKUP)

Hope that helps

/AJ
 
Upvote 0
Sorry I have no clue how to get it to work.
 
Upvote 0
Once you've copied the code in to a module in your workbook, it basically installs a new Excel Function that you can use in a worksheet as you would any normal function, like VLOOKUP.

/AJ
 
Upvote 0
Thanks for your help but its going totally over my head. Either nothing happens or I get #VALUE. I need an idiot proof macro where I run it and it does it all for me!!!
 
Upvote 0
I have tried the copy paste special transpose but that put all the values in separate cells.
 
Upvote 0
Try this:-
Results sheet2
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Nov22
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A8"), Range("A" & Rows.Count).End(xlUp))
[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 .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Offset(1).Value
    [COLOR="Navy"]Else[/COLOR]
        .Item(Dn.Value) = .Item(Dn.Value) & "/ " & Dn.Offset(, 1).Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
Sheets("sheet2").Range("A2").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick, I run it and it comes up with 'Type Mismatch'?
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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