Add, remove and sort

Mann750

Board Regular
Joined
Dec 16, 2009
Messages
72
Hi,

I have a few queries that I need help on and they all need to be applied to a set of data that changes every month.

I am able to download a delivery schedule from another source into Excel with a number of variables that need to be sorted into a more efficient way for variance analysis. Some of the cells are merged together but they are not always merged in a consistant manner. Also, I need to add the contract numbers to the list instead of having it displayed seperately. It will be easier to understand what I mean if I could attach my file but basically I would like this:

HTML:
Contract 01/001
 
Delivery Date   Product  Sales  Person
 
Contract 02/001
 
Delivery Date   Product  Sales  Person

to become this:

HTML:
Contract  Delivery Date  Product  Sales  Person
01/001    
02/001

I hope this makes sense and I would greatly appreciate any help.

Many thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I have had to do something similar in the past. Here is the easiest way I found (but there may be better).
1. Unmerge all the the cells.
2. Record a macro while you make the changes you want on the first record.
3. Stop the macro and then put a loop around it to complete the same action for the rest.
 
Upvote 0
Hi VBACO, thanks for the suggestion. I have tried that method and it does work but the only problem is that every month the data is completely different in terms of information and layout so the macro doesn't always work. Also, because there is a lot of information to go through it would take me a while to go through each contract with a different macro. I will try again with macro recording and see if I can simplify the situation further.

Cheers!
 
Upvote 0
Hmm, that does make it more difficult. I will see if I can come up with a more dynamic macro, but it probably wont be done today. Hopefulyl someone better than I can help you out sooner.
 
Upvote 0
Mann750,

Can we have another workbook at Box.Net that contains samples of the other raw data worksheet styles/formats?

And, what is the actual name of an original raw data worksheet?
 
Upvote 0
Thanks VBACO, really appreciate it :o)

I've made a bit of progress with adding the contract numbers but I am having difficulty with stopping the loop...any ideas?

Code:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
        Cells.Select
    With Selection
        .WrapText = False
        .MergeCells = False
    End With
    Selection.ColumnWidth = 8.43
    Selection.RowHeight = 12.75
    Range("A1").Select
    
Dim finddel As Range, finddel1 As Range, findtot As Range
    
    Set finddel = Range("B1:B55").Cells
    Range("B1").Select
    
    Set finddel1 = finddel.Find(what:="Delivery Date", after:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole)
        
    Do Until finddel1 Is Nothing
    ' This keeps running by going back to the beginning of column B and starting the process again
       
        Set findtot = finddel.Find(what:="Total", after:=finddel1, LookIn:=xlValues, LookAt:=xlWhole)
            finddel1.Offset(-3, 1).Select
            Selection.Copy
            Range(finddel1.Offset(2, -1), findtot.Offset(-1, -1)).PasteSpecial xlPasteValues
            findtot.Select
            
        Set finddel1 = finddel.Find(what:="Delivery Date", after:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole)
        
    Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Mann750,


It's always better to see the actual raw data. Thanks.


What would be the MAX number of rows of data on worksheet Customer Delivery Schedule?
 
Upvote 0
Mann750,


Interesting problem with the data not lining up thru all the groups.


Sample raw data (not all data shown because of the number of rows):


Excel Workbook
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
22
23Contract :02/001
24
25
26Delivery DateProductSales Order No.PersonProductProductProductCodeOutletShip ToStatusSystem
27QuantityTypeQuantityTypeQuantityType
2812/1/20126,487.435###11489MM######ABC66,911.405113.00046,067.276PWWWWWWNOT DONE5
29Total6,487.435######66,911.40546,067.2765
30
Customer Delivery Schedule





Excel Workbook
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAW
48
49Contract :01/120
50
51
52Delivery DateProductSales Order No.PersonProductProductProductCodeOutletShip ToStatusSystem
53QuantityTypeQuantityTypeQuantityType
5412/1/2012300.000##########AA1,375.800ABC1,075.8001,124.000815.400XGGGT.B.A.DONE1
5512/1/2012500.000##########AA3,142.500ABC2,642.5001,124.0002,152.500XGGGT.B.A.DONE1
5612/1/20121,700.000##########AA#######ABC1,151,117.0001,124.0009,775.000XGGGT.B.A.DONE2
Customer Delivery Schedule





Excel Workbook
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
114
115Contract :03/004
116
117
118Delivery DateProductSales Order No.PersonProductProductProductCodeOutletShip ToStatusSystem
119QuantityTypeQuantityTypeQuantityType
12012/15/20142,662.761###10641MM######ABC#########1,127.00020,000.000XGGGT.B.A.NOT DONE2
12112/15/2015399,123,142.000###10640MM######ABC38,391.6931,127.00030,000.000XGGGT.B.A.NOT DONE3
12212/15/20166,656.903###10639MM######ABC63,986.1521,127.00050,000.000XGGGT.B.A.NOT DONE5
Customer Delivery Schedule





After the macro in a new worksheet Finished:


Excel Workbook
BCDEFGHIJKLMNOPQ
2ContractDelivery DateProduct QuantityProduct TypeSales Order No.PersonProduct QuantityProduct TypeProduct QuantityProduct TypeProductCodeOutletShip ToStatusSystem
301/0012/1/20113,900.0001,156.00010134MM39,281,237.000ABC35,381,237.000113.00023,848.500SWWWWWWDONE3
401/0019/1/20113,973.0001,237.00012116MM4,253,123,938.000ABC38,561.938113.00026,285.368CAAAAAADONE3
501/0012/1/20124,390.00012,365.00012103MM46,463.760ABC42,073.760113.00028,622.800XWWWWWWDONE3
601/0012/1/2012360.0001,156.00012746MM3,626.280ABC3,266.280113.0002,201.400XAAAAAADONE1
701/0018/1/20123,600.0001,156.00012748MM36,262.800ABC32,662.800113.00022,014.000XAAAAAADONE3
801/00111/1/20135,400.0001,156.00012753MM47,266.200ABC41,866.2001,123.00038,032.200XGGGT.B.A.DONE4
901/0018/1/20146,120.0001,156.00012755MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.NOT DONE5
1001/00111/1/20146,120.0001,156.00012757MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.5
1101/0018/1/20154,500.0001,156.00011849MM39,388.500ABC34,888.5001,123.00031,693.500XGGGT.B.A.NOT DONE3
1201/00111/1/20156,120.0001,156.00011850MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.NOT DONE5
1301/00111/1/20166,120.0001,156.00011852MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.NOT DONE5
1401/00111/1/20176,120.0001,156.00012759MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.5
1501/00111/1/20186,120.0001,156.00012761MM53,568.360ABC47,448.3601,123.00043,103.160XGGGT.B.A.5
1601/00111/1/20191,272.6731,156.00012763MM11,139.707ABC9,867.0341,123.0008,963.435XGGGT.B.A.NOT DONE1
1702/00112/1/20126,487.4352,016.00011489MM73,398.840ABC66,911.405113.00046,067.276PWWWWWWNOT DONE5
1803/0051/4/201122,725.0001,954.00010602MM22,945,123,325.000ABC206,729.325113.000139,417.875CAAAAAADONE15
1905/10110/1/20112,150.0001,233.00011842MM19,351,233.000ABC17,201,233.000113.00011,343.400CAAAAAADONE2
2005/10110/1/20118,704.0001,234.00012023MM86,831.104ABC78,127.104113.00052,563.456CWWWWWWDONE6
2105/1018/1/201236,900.0001,234.00011843MM368,111,234.000ABC331,211,234.000113.000222,839.100CAAAAAANOT DONE25
2201/12012/1/2012300.0001,231.00010032AA1,375.800ABC1,075.8001,124.000815.400XGGGT.B.A.DONE1
2301/12012/1/2012500.0001,232.00010033AA3,142.500ABC2,642.5001,124.0002,152.500XGGGT.B.A.DONE1
2401/12012/1/20121,700.0001,233.00010034AA1,321,117.000ABC1,151,117.0001,124.0009,775.000XGGGT.B.A.DONE2
2501/12012/1/2012500.0001,234.00010035AA4,416.000ABC3,916.0001,124.0003,402.500XGGGT.B.A.DONE1
2601/12012/1/20121,800.0001,239.00010037AA17,809.200ABC16,009.2001,124.00014,175.000XGGGT.B.A.DONE2
Finished





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 08/18/2011
' http://www.mrexcel.com/forum/showthread.php?t=572682
Dim wC As Worksheet, wF As Worksheet, wT As Worksheet
Dim LR As Long, LC As Long, LC2 As Long, a As Long, n As Long, LRT As Long
Dim Area As Range, SR As Long, ER As Long, EC As Long, NR As Long
Application.ScreenUpdating = False
Set wC = Worksheets("Customer Delivery Schedule")
If Not Evaluate("ISREF(Finished!A1)") Then Worksheets.Add(After:=wC).Name = "Finished"
Set wF = Worksheets("Finished")
wF.UsedRange.Clear
wC.UsedRange.Copy wF.Range("A1")
With wF.UsedRange
  .WrapText = False
  .Orientation = 0
  .AddIndent = False
  .ShrinkToFit = False
  .ReadingOrder = xlContext
  .MergeCells = False
End With
On Error Resume Next
wF.Range("C1", wF.Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
LR = wF.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
LC = wF.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
For a = LC To 1 Step -1
  n = Application.CountA(wF.Range(wF.Cells(1, a), wF.Cells(LR, a)))
  If n = 0 Then wF.Columns(a).Delete
Next a
wF.UsedRange.Columns.AutoFit
wF.Rows(1).Insert
For a = LR To 2 Step -1
  If wF.Cells(a, 1) = "Total" Then wF.Rows(a).Offset(1).Insert
Next a
Worksheets.Add().Name = "Temphiker95"
Set wT = Worksheets("Temphiker95")
For Each Area In wF.Range("B1", wF.Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    SR = .Row
    ER = SR + .Rows.Count - 1
    wT.UsedRange.Clear
    EC = wF.Cells(ER, Columns.Count).End(xlToLeft).Column
    wF.Range(wF.Cells(SR, 1), wF.Cells(ER, EC)).Copy wT.Range("A1")
    LRT = wT.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
    For a = EC To 1 Step -1
      n = Application.CountA(wT.Range(wT.Cells(1, a), wT.Cells(LRT, a)))
      If n = 0 Then wT.Columns(a).Delete
    Next a
    wF.Range(wF.Cells(SR, 1), wF.Cells(ER, EC)).Clear
    wT.UsedRange.Copy wF.Range("A" & SR)
  End With
Next Area
LC2 = wF.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
For a = LC To LC2 + 1 Step -1
  wF.Columns(a).Delete
Next a
wF.Cells(2, LC2 + 2).Resize(, 16) = [{"Contract","Delivery Date","Product Quantity","Product Type","Sales Order No.","Person","Product Quantity","Product Type","Product Quantity","Product Type","Product","Code","Outlet","Ship To","Status","System"}]
For Each Area In wF.Range("B1", wF.Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    SR = .Row
    ER = SR + .Rows.Count - 1
    NR = wF.Cells(wF.Rows.Count, LC2 + 2).End(xlUp).Offset(1).Row
    wF.Range("B" & SR).Copy wF.Cells(NR, LC2 + 2).Resize(ER - 1 - SR - 2)
    wF.Range(wF.Cells(SR + 3, 1), wF.Cells(ER - 1, LC2)).Copy wF.Cells(NR, LC2 + 3)
  End With
Next Area
For a = LC2 To 1 Step -1
  wF.Columns(a).Delete
Next a
Application.DisplayAlerts = False
wT.Delete
Application.DisplayAlerts = True
LR = wF.Cells(Rows.Count, 2).End(xlUp).Row
LC = wF.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
With wF.Range("B3:B" & LR)
  .Font.Name = "Arial"
  .Font.FontStyle = "Regular"
  .Font.Size = 9
  With .Borders(xlEdgeLeft)
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = 24
  End With
  With .Borders(xlEdgeTop)
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = 24
  End With
  With .Borders(xlEdgeBottom)
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = 24
  End With
  With .Borders(xlEdgeRight)
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = 24
  End With
  With .Borders(xlInsideHorizontal)
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = 24
  End With
End With
wF.Range(wF.Cells(3, 2), wF.Cells(LR, LC)).HorizontalAlignment = xlCenter
With wF.Range("B2:Q2")
  .HorizontalAlignment = xlCenter
  .VerticalAlignment = xlCenter
  .Font.Name = "Arial"
  .Font.FontStyle = "Bold"
  .Font.Size = 9
  .Font.ColorIndex = 2
  .Interior.ColorIndex = 47
End With
wF.UsedRange.Columns.AutoFit
wF.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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