Hello,
I am looking for a help in either vba or excel formulas to convert a set of categorized data vertically to horizontal by category.
Appreciate the support.
attached photo for an example:
Capture.PNG
Hello,
I am looking for a help in either vba or excel formulas to convert a set of categorized data vertically to horizontal by category.
Appreciate the support.
attached photo for an example:
Capture.PNG
Last edited by whallak; 02-16-2019 at 12:47 PM.
Try this code
Similar issue at this link![]()
Sub Test() Dim a, v, i As Long, c As Integer c = 5 'Column E a = Range("A3:B" & Cells(Rows.Count, 1).End(xlUp).Row).Value With CreateObject("Scripting.Dictionary") .CompareMode = 1 For i = LBound(a) To UBound(a) If .Exists(a(i, 2)) Then .Item(a(i, 2)) = .Item(a(i, 2)) & Chr(2) & a(i, 1) Else .Item(a(i, 2)) = a(i, 2) & Chr(2) & a(i, 1) End If Next i Application.ScreenUpdating = False For Each v In .Items Cells(2, c).Resize(UBound(Split(v, Chr(2))) + 1).Value = Application.Transpose(Split(v, Chr(2))) c = c + 1 Next v Application.ScreenUpdating = True End With End Sub
https://www.excelforum.com/excel-pro...sheet-2-a.html
< ----- Please click the little star * next to add reputation if my post helps you
Visit Forum : From Here
Thank you Yasser, this was very helpful.
I noticed that once the data are plotted on Column E, they are plotted by order of existence as available in range B.
Would it be possible to have them plotted by alphabetical order ?
Instead of having A,C,B,D,E
to have A,B,C,D,E, etc...
check the attached photoCapture.PNG
This macro assumes that your headers are in row 1 and the data starts in row 2. The result will be placed in Sheet2.
![]()
Sub whallak() Application.ScreenUpdating = False Dim LastRow As Long, cat As Range, RngList As Object, item As Variant, lCol As Long, desWS As Worksheet LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Set desWS = Sheets("Sheet2") lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column Set RngList = CreateObject("Scripting.Dictionary") For Each cat In Range("B2", Range("B" & Rows.Count).End(xlUp)) If Not RngList.Exists(cat.Value) Then RngList.Add cat.Value, Nothing End If Next cat For Each item In RngList desWS.Cells(1, lCol) = item Range("A1:B" & LastRow).AutoFilter Field:=2, Criteria1:=item Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy desWS.Cells(Rows.Count, lCol).End(xlUp).Offset(1, 0) lCol = lCol + 1 Range("A1").AutoFilter Next item Application.ScreenUpdating = True End Sub
You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
Practice makes perfect. I'm very far from perfect so I'm still practising.
Can you attach a sample workbook so as to be easier to work on?
here is the workbook
Thank you
Try
![]()
Sub test() Dim x, y, i As Long, LastR As Long LastR = Range("b" & Rows.Count).End(xlUp).Row x = Filter(Evaluate("transpose(if(countif(offset(b3:b" & LastR & ",,,row(1:" & _ LastR & ")),b3:b" & LastR & ")=1,b3:b" & LastR & "))"), False, 0) With [e2].Resize(, UBound(x) + 1) .Value = x For i = 1 To .Columns.Count y = Filter(Evaluate("transpose(if(b3:b" & LastR & "=" & _ .Cells(1, i).Address & ",a3:a" & LastR & "))"), False, 0) .Cells(2, i).Resize(UBound(y) + 1).Value = Application.Transpose(y) Next End With End Sub
Get the Power Query add-in - it will be simple then.
Ali
Enthusiastic self-taught user of MS Excel who's always learning!
Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.
NB: as a Moderator, I never accept friendship requests.
Forum Rules (updated August 2023): please read them here.
![]()
Sub test() Dim a, b, i As Long, n As Long, maxRow As Long With Sheets("item category") a = .Range("a7", .Range("a" & Rows.Count).End(xlUp)).Resize(, 5).Value End With ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 100) With CreateObject("Scripting.Dictionary") .CompareMode = 1 For i = 1 To UBound(a, 1) If Not .exists(a(i, 5)) Then .Item(a(i, 5)) = Array(1, .Count + 1) If UBound(b, 2) < .Count Then ReDim Preserve b(1 To UBound(b, 1), 1 To UBound(b, 2) + 100) End If b(1, .Count) = a(i, 5) End If n = .Item(a(i, 5))(0) + 1 b(n, .Item(a(i, 5))(1)) = a(i, 2) .Item(a(i, 5)) = Array(n, .Item(a(i, 5))(1)) If maxRow < n Then maxRow = n Next ReDim Preserve b(1 To UBound(b, 1), 1 To .Count) End With With Sheets.Add.Cells(1).Resize(maxRow, UBound(b, 2)) .Value = b .Columns.AutoFit End With End Sub
Last edited by jindon; 02-18-2019 at 05:48 AM.
Alphabetical order?
![]()
Private Sub CommandButton1_Click() Dim a, b, i As Long, n As Long, maxRow As Long a = Me.Range("a7", Me.Range("a" & Rows.Count).End(xlUp)).Resize(, 5).Value ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 100) With CreateObject("Scripting.Dictionary") .CompareMode = 1 For i = 1 To UBound(a, 1) If Not .exists(a(i, 5)) Then .Item(a(i, 5)) = Array(1, .Count + 1) If UBound(b, 2) < .Count Then ReDim Preserve b(1 To UBound(b, 1), 1 To UBound(b, 2) + 100) End If b(1, .Count) = a(i, 5) End If n = .Item(a(i, 5))(0) + 1 b(n, .Item(a(i, 5))(1)) = a(i, 2) .Item(a(i, 5)) = Array(n, .Item(a(i, 5))(1)) If maxRow < n Then maxRow = n Next ReDim Preserve b(1 To UBound(b, 1), 1 To .Count) End With With Me.[h6].Resize(maxRow, UBound(b, 2)) .CurrentRegion.ClearContents .Value = b .Sort .Rows(1), 1, Orientation:=2 .Columns.AutoFit End With End Sub
yes if you check the below photo, the plotting in the new sheet starts with the first category, and not by alphabetical order (a,b,c,d,...)
check the photo:
Capture.PNG
1)
Don't quote whole posts -- it's just clutter.
If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding
For normal conversational replies, try using the QUICK REPLY box below.
2) Which one are you talking about?
Wonderful,
Thank you for the support, and also for the guidance. That is my second day here, good to know such guidelines.
Regards,
You are welcome and thanks for the rep.
If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks