Vba code to calculate line items after sort

Cosmo64

New Member
Joined
Nov 18, 2009
Messages
1
Hi,

This is my first post, but have been a long time viewer and gather of information from this forum.

I currently have a report produced which gives me a list of customers, accounts , products, sales managers and the expiry date of that particular contract. This report produced over a three month date range can be up to 20,000 lines of data. Whilst the report itself is sufficient for sales managers, I want to be able to summarize the content of this report to a higher level.

One contract may have multiple lines of data due to multiple accounts etc so 100 line items on the report may indicate only one contract. I need to be able to summarize this data on another spreadsheet to show me numbers of contracts per sales team and expiry dates. So this would be a calculation based on column D,G and H. This will form the basis of a report giving an overview of what is outstanding.

Currently I have built a macro which sorts the original data and if the same product is attributed to the same customer but has a different account, the account is appended to the above cell and the below line is deleted. So 20,000 lines of data could reduce to about 8000. I need to then summarize those 8000 lines per sales team and expiry date. I was thinking of pivot tables but I would like to have it all done via a macro at the same time.
An example of my data is below and the code I am currently using

Sub appendcontracts()
Dim s, e As Long


ActiveSheet.Copy




'sort expiry date (7), cust number (3), product (4)"


Cells.Sort Key1:=Columns(7), key3:=Columns(4), Header:=xlYes, key2:=Columns(3)


s = 2
e = 2
While (Cells(e + 1, 3) <> "")

'If the customer number or product does not equal the cell above, append the account number up a cell
If Cells(e + 1, 3) <> Cells(e, 3) Or Cells(e + 1, 4) <> Cells(e, 4) Then
'changed, append account number to the first (s)
For i = s + 1 To e
If InStr(1, Cells(s, 1), Cells(i, 1)) = 0 Then
Cells(s, 1) = Cells(s, 1) & ", " & Cells(i, 1)
End If

Next i

'delete rows once account has been appended
For i = s + 1 To e
Rows(s + 1).Delete
Next i
s = s + 1

's = e + 1

e = s

Application.ScreenUpdating = False

Else
e = e + 1
End If

Wend


' this may be old code no longer required
If Cells(e + 1, 3) <> Cells(e, 3) Or Cells(e + 1, 4) <> Cells(e, 4) Then
'changed, append to the first (s)
For i = s + 1 To e
If InStr(1, Cells(s, 1), Cells(i, 1)) = 0 Then
Cells(s, 1) = Cells(s, 1) & ", " & Cells(i, 1)
End If

Next i

'delete rows
For i = s + 1 To e
Rows(s + 1).Delete
Next i
s = s + 1

's = e + 1

e = s
'Cells(s, 1).Activate
Application.ScreenUpdating = False

Else
e = e + 1
End If


End Sub

The spreadsheet after running this code looks like

Accountcontract nbrcustomer numberProductCustomer Nameacct mgrexpiry dateTeam
9940144460125426ApplesACMERep A30/06/2015Yellow
1, 2, 3, 440564160125426OrangesACMERep A30/06/2015Yellow
35, 3650645660126958PineapplesTESTING CORep C30/06/2015Red
3250456460128584ApplesABCDRep B31/07/2015Blue
3350458960128584BananasABCDRep B31/07/2015Blue
2450545660123659SteelBrown CoRep D31/08/2015Green
23561525154660123959WoodBrown CoRep E30/09/2015White
264615, 6725461760125874PlasticMYDE Pty LtdRep F30/10/2015Black
154155, 154255301254560125000SandWork CoRep G30/11/2015Red
51301258860125000WoodWork CoRep G30/11/2015Red

<tbody>
</tbody>
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,215,330
Messages
6,124,305
Members
449,150
Latest member
NyDarR

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