Product/Buyers to rows and columns

AkmalMir

New Member
Joined
Mar 11, 2017
Messages
4
Hello, It is nice to join Excel community, I'm new here if there some mistakes don't blame me please.

I have a task to show sales, products to buyers (what kind of products buyers usually buy), here is the task(my data unloaded from software in such way):

I would be very thankful if you could show the way out.
Buyer/Productunitquantitysales
Shop A113
appleskg25
pearskg15
Shop B5
lemonkg35
Shop C5
melonspiece35
etc.(over 500 rows)
How to transform it to automatically?
Shop AShop BShop C
apples5
pears5
lemon5
melons5

<tbody>
</tbody>

 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How can one determine if "Shop A" is a location or a product?
Are there any products that have a 0 value? Do you want them included in the rearrangement?
 
Upvote 0
How to transform it to automatically?
BuyerProductunitquantitysales Shop AShop BShop C
Shop Aappleskg24apples400
Shop Apearskg16pears600
Shop Blemonkg35lemon050
Shop Cmelonspiece35melons005
Shop Cplumskg69plums009
Shop Corangeskg711oranges0011
by tidying up your data in the first tableformula giving sales of 4 for shop A apples is
the right hand table is auto populated
=SUMPRODUCT(($B$3:$B$8=$I3)*($A$3:$A$8=J$2)*($E$3:$E$8))
I think tidying could be done
automaticallydragged down and across
with amacro
zeros can be replaced by blanks if desired

<colgroup><col><col><col><col><col span="4"><col><col span="6"></colgroup><tbody>
</tbody>
 
Upvote 0
sorry forgot to enter, 1 in units, by one in units; no there isn't, no need , here is corrected vers.
102ro8m.jpg
 
Upvote 0
oldbrewer ok but there is 500 rows, it is very labour intensive work, is there any other easier way out?(added it's original screenshot)
 
Upvote 0
I think this will do what you want. Adjust rngInput and rngOutput to suit.

Code:
Sub test()
    Dim arrOutPut() As String
    Dim arrProducts() As String, arrBuyers() As String
    Dim ProductPointer As Variant, BuyerPointer As Long, ProductCount As Long
    Dim DataCount As Long
    Dim rngInput As Range
    Dim rngOutput As Range
    Dim i As Long, strX As String
    
    Set rngInput = Sheet1.Range("A1"): Rem cell that holds "Buyer/Product"
    Set rngOutput = Sheet1.Range("Q1"): Rem adjust
    
    With rngInput
        Set rngInput = Range(.Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))
    End With
    DataCount = rngInput.Cells.Count
    ReDim arrOutPut(1 To DataCount, 1 To DataCount)
    ReDim arrProducts(1 To DataCount)
    ReDim arrBuyers(1 To DataCount)
    BuyerPointer = 1
    ProductCount = 1
    
    For i = 1 To DataCount
        strX = StrConv(CStr(rngInput.Cells(i, 1).Value), vbProperCase)
        
        If rngInput.Cells(i, 2) = 1 Then
            Rem strX is buyer
            BuyerPointer = BuyerPointer + 1
            arrOutPut(1, BuyerPointer) = strX
        Else
            Rem strX is product
            Rem if is existing product
            ProductPointer = Application.Match(strX, arrProducts, 0)
            If IsError(ProductPointer) Then
                Rem add to product list
                ProductCount = ProductCount + 1
                arrOutPut(ProductCount, 1) = strX
                arrProducts(ProductCount) = strX
                ProductPointer = Application.Match(strX, arrProducts, 0)
            End If
            arrOutPut(ProductPointer, BuyerPointer) = rngInput.Cells(i, 4) + Val(arrOutPut(ProductPointer, BuyerPointer))
        End If
    
    Next i
    
    With rngOutput.Resize(ProductCount, BuyerPointer)
        .Value = arrOutPut
    End With
End Sub
 
Upvote 0
Mike has nailed it, but as I said in my post one macro could re-arrange the data and the second could make the table you want...
 
Upvote 0
If you wanted to try another code ..

(I have assumed no blank cells in the left hand column of the data area - column A in my screen shot below)

Rich (BB code):
Sub Buyers_Products()
  Dim dBuyers As Object, dProducts As Object
  Dim aData As Variant, aResults As Variant
  Dim i As Long, j As Long, k As Long
  Dim sBuyer As String, sProduct As String
  
  Const sTopleftData As String = "A1"     '<- Set to top left cell of data area
  Const sTopLeftResults As String = "G1"  '<- Set to top left cell of results arae
  
  Set dBuyers = CreateObject("Scripting.Dictionary")
  dBuyers.CompareMode = 1
  Set dProducts = CreateObject("Scripting.Dictionary")
  dProducts.CompareMode = 1
  aData = Range(sTopleftData, Range(sTopleftData).End(xlDown)).Resize(, 4).Value
  ReDim aResults(1 To UBound(aData), 1 To UBound(aData))
  For i = 2 To UBound(aData)
    If IsNumeric(aData(i, 2)) Then
      sBuyer = aData(i, 1)
      If Not dBuyers.exists(sBuyer) Then
        j = j + 1: dBuyers(sBuyer) = j
      End If
    Else
      sProduct = aData(i, 1)
      If Not dProducts.exists(sProduct) Then
        k = k + 1: dProducts(sProduct) = k
      End If
      aResults(dProducts(sProduct), dBuyers(sBuyer)) = aResults(dProducts(sProduct), dBuyers(sBuyer)) + aData(i, 4)
    End If
  Next i
  With Range(sTopLeftResults)
    .Offset(1, 1).Resize(dProducts.Count, dBuyers.Count).Value = aResults
    .Offset(, 1).Resize(, dBuyers.Count).Value = dBuyers.Keys
    .Offset(1).Resize(dProducts.Count).Value = Application.Transpose(dProducts.Keys)
  End With
End Sub

My data & results:


Excel 2010 32 bit
ABCDEFGHIJ
1Buyer/ProductunitquantitysalesShop AShop BShop C
2Shop A113apples5
3appleskg25pears5
4pearskg15lemon5
5Shop B15melons5
6lemonkg35
7Shop C15
8melonspiece35
Products Buyers
 
Last edited:
Upvote 0
That version worked for me(beginner). Thanks to All people and especially Peter_SSs. Could you give an expensive advice how i can learn VBA in Excel, any youtube pages?)
 
Upvote 0

Forum statistics

Threads
1,215,130
Messages
6,123,220
Members
449,091
Latest member
jeremy_bp001

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