How can this be done??

Johny1900

Board Regular
Joined
Dec 20, 2007
Messages
54
Hi,

I have a table with articles which have been bought during this year. The structure is:

Number_art, description,discount

Now in this table the article can be there in several lines. What I want to achieve is to get as a result

Number_art, description,discount1,discount2

Where discount1 is the latest discount and discount2 is th second latest discount.
Example in data

Table org.
Code:
1234 VHS 10 
1234 VHS  5  
1234 VHS 11 
2222 VCR   5 
2223 BETA 10
2223 BETA  5

the result should be

Code:
1234 VHS 10 5 
2222 VCR   5  
2223 BETA 10 5


getting the data is no problem, but just getting the Discount of the second row and adding it there is getting me a headache..... any help is very welcome!!!

thx

Johny
 

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
Code:
Sub test()
Dim a, b(), w(), i As Long, z As String, n As Long
With Range("a1").CurrentRegion.Resize(,3)
    a = .Value
    ReDim b(1 To UBound(a,1), 1 To Columns.Count)
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextcompare
        z = a(i,1) & ";" & a(i,2)
        For i = 2 To UBound(a,1)
            If Not .exists(z) Then
                n = n + 1 : b(n,1) = a(i,1) : b(n,2) = a(i,2)
                .add z, Array(n, 2)
            Else
            w = .item(z) : w(1) = w(1) + 1
            b(w(0), w(1)) = a(i,3)
            maxCol = Application.Max(maxCol, w(1))
            .item(z) = w
        Next
    End With
    .Offset(, .Columns.Count + 1).Resize(n, maxCol).Value = b
End With
End Sub
 
Upvote 0
Jindon,

I tried you code this morning because I needed something simular and I get the following errors.

Compile Error: Next without For

probably because an end if is missing,

Then I get

subscript out of range z = a(i, 1) & ";" & a(i, 2)
 
Upvote 0
Jindon,

I tried you code this morning because I needed something simular and I get the following errors.

Compile Error: Next without For

probably because an end if is missing,

Then I get

subscript out of range z = a(i, 1) & ";" & a(i, 2)
Stephen,
1) Yes, Else should be End If
2) change
Rich (BB code):
        z = a(i,1) & ";" & a(i,2)
        For i = 2 To UBound(a,1)
to
Rich (BB code):
        For i = 2 To UBound(a,1)
            z = a(i,1) & ";" & a(i,2)
 
Upvote 0
That was it Jindon,

Thanks again, your code was very usefull!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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