Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

sort sheets using macro ?

This is a discussion on sort sheets using macro ? within the Excel Questions forums, part of the Question Forums category; Hi again made the changes you suggested Thanks for taking the time to look at this. I am still getting ...

  1. #11
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Hi again

    made the changes you suggested

    Thanks for taking the time to look at this.

    I am still getting errors when I run this
    see starred comment
    I must be doing something wrong.

    Your comment re bad data structure unfortunately the imported file comes that way I suppose I could add a line to the macro to insert a heading above the data on c4 called WSID


    Code:
    Sheets("2").Name = "NAR"
        Sheets("3").Name = "SRV"
        Sheets("4").Name = "SAR"
        Sheets("5").Name = "ERV"
        Sheets("6").Name = "WRV"
        Sheets("7").Name = "A92"
        Sheets("8").Name = "LAN"
        Sheets("9").Name = "OVH"
        Sheets("10").Name = "NEN"
        Sheets("11").Name = "SEN"
        Sheets("12").Name = "PLP"
        Sheets("13").Name = "M80"
        Sheets("15").Name = "NMI"
        Sheets("16").Name = "SMI"
        Sheets("19").Name = "NWC"
        Sheets("20").Name = "SWC"
        Sheets("23").Name = "NBR"
        Sheets("24").Name = "SBR"
        Sheets("26").Name = "NRV"
        Sheets("27").Name = "LIG"
            
         Dim wsEachSheet             As Worksheet
        Dim IngLastRow              As Long
    
        For Each wsEachSheet In Worksheets
            With wsEachSheet
                IngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                
                .Range("AN:AP,AR:AT,AW:BA").Interior.ColorIndex = 34
                .Range("AN:AP,AR:AT,AW:BA").Pattern = xlSolid
      * Invalid use of property*   .Range ("BC:BE,BV:BV,BX:BZ,CC:CD,CG:CK,CM:CM")
                    .FormatConditions.Delete
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="0"
                    .FormatConditions(1).Font.ColorIndex = xlAutomatic
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="0"
                    .FormatConditions(2).Font.ColorIndex = 5
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="0"
                    .FormatConditions(3).Font.ColorIndex = 3
                
               
        
                Columns ("BF:BF")
                FormatConditions.Delete
                FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
                Formula1:="=$BH5"
                FormatConditions(1).Font.ColorIndex = 10
                FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                Formula1:="=$BH5"
                FormatConditions(2).Font.ColorIndex = 9
                
    
                .Range("C5:C" & IngLastRow).Formula = "=VLOOKUP(RC[-2],'[3G lOOKUP.xls]ALL'!C3:C5,3,FALSE)"
                .Range("U5:U" & IngLastRow).Formula = "=VLOOKUP(RC[-2],'[3G lOOKUPV.xls]ALL'!C3:C5,6,FALSE)"
                .Range("T5:T" & IngLastRow).Formula = "=IF(RC[-10]=0,IF(R[1]C[-3]=0,0,1),1)"
                .Range("A5:CM5" & IngLastRow).Sort Key1:=.Range("C5"), Order1:=xlAscending, Header:=xlNo, _
                    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortTextAsNumbers
                .Range("A5:CM5" & IngLastRow).Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, _
                    8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 83, 85, 86, 87, 88, 89, 90, 91), _
                    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    
                .Rows("4:4").AutoFilter
                .Range("T5:T" & IngLastRow).AutoFilter Field:=20, Criteria1:="1"
                
                Columns("D:AL").Select
        Selection.EntireColumn.Hidden = True
        
            End With
        Next
        
        End Sub
    Seriously beginning to wonder if I am ever going to get this to work, I am positive it can be done but having spent hours and hours poring over this and still not getting the desired result am definately becoming disheartened.

    Hats off to you guys who can understand and make this stuff work


    Thanks again

    Richard
    Last edited by Zack Barresse; May 1st, 2012 at 04:37 PM. Reason: Added CODE tags

  2. #12
    GTO
    GTO is offline
    Board Regular
    Join Date
    Dec 2008
    Location
    Phoenix, Arizona
    Posts
    4,597

    Default Re: sort sheets using macro ?

    Greetings,

    You may have glossed over Zack's request...

    Quote Originally Posted by Zack Barresse View Post
    I added CODE tags to your code. Please use them in the future for readability purposes.
    ...
    The code tags really do make reading the post much easier

    [code] '//Your code between the tags// [/code]

    Mark

  3. #13
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Sorry

    code tags ???

    I dont know what these are.

    All and any help gratefully recieved

    Richard

  4. #14
    GTO
    GTO is offline
    Board Regular
    Join Date
    Dec 2008
    Location
    Phoenix, Arizona
    Posts
    4,597

    Default Re: sort sheets using macro ?

    Hi Richard,

    The code tags are simply "tags" between brackets that are interpreted when you submit the post. In the line before my name in my last post, I showed how you would type the tags to get your post to render correctly. Does that make sense?

    Mark

    Edit:

    If my verbiage is a bit confusing, try this.

    Click the "quick reply" icon, then the 'Go Advanced' button. See the little pound sign in the upper right? Click this and you'll see the CODE tags inserted. Your code simply goes between the start/end tags.
    Last edited by GTO; May 1st, 2012 at 08:11 AM.

  5. #15
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Thanks for info.

    I understand now
    will use them in future

    Rickf

  6. #16
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Hi again

    below is further attempt at code
    I have entered the error messages I get
    all of these appear before anything is done on the sheets
    I had to change it a bit cos I read that headings were required for subtotal to work which meant delete row 4 and rewrite all refs to row5 as 4
    I am really struggling to get this working

    I need it to do the following

    Downloaded file from External source has 24 to 27 sheets (200+mb)
    all data needs sorted by col c (info from vlookup)
    all data then subtotalled by col c
    data col u from vlookupv
    formula entered into col t (current info =1)
    autofilter col t by 1 to show only current data
    various conditional formatting to highlight variances

    all sheets have varying amounts (rows) of data

    Thanks for help already given
    Sorry to keep asking




    Code:
     '//YSub step1()
    '
    ' step1 Macro
    ' rename tabs and reformat columns
    '
    ' Keyboard Shortcut: Ctrl+a
    '
        
        Sheets("2").Name = "NAR" SUBSCRIPT OUT OF RANGE
        Sheets("3").Name = "SRV"
        Sheets("4").Name = "SAR"
        Sheets("5").Name = "ERV"
        Sheets("6").Name = "WRV"
        Sheets("7").Name = "A92"
        Sheets("8").Name = "LAN"
        Sheets("9").Name = "OVH"
        Sheets("10").Name = "NEN"
        Sheets("11").Name = "SEN"
        Sheets("12").Name = "PLP"
        Sheets("13").Name = "M80"
        Sheets("15").Name = "NMI"
        Sheets("16").Name = "SMI"
        Sheets("19").Name = "NWC"
        Sheets("20").Name = "SWC"
        Sheets("23").Name = "NBR"
        Sheets("24").Name = "SBR"
        Sheets("26").Name = "NRV"
        Sheets("27").Name = "LIG"
            
         Dim wsEachSheet             As Worksheet
        Dim IngLastRow              As Long
    
        For Each wsEachSheet In Worksheets
            With wsEachSheet
                IngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                
                
                
                           
    
                .Range("C4:C" & IngLastRow).Formula = "=VLOOKUP(RC[-2],'[3G lOOKUP.xls]ALL'!C3:C5,3,FALSE)"
                .Range("U4:U" & IngLastRow).Formula = "=VLOOKUP(RC[-2],'[3G lOOKUPV.xls]ALL'!C3:C5,6,FALSE)"
                .Range("T4:T" & IngLastRow).Formula = "=IF(RC[-10]=0,IF(R[1]C[-3]=0,0,1),1)"
                .Range("A4:CM4" & IngLastRow).Sort Key1:=.Range("C3"), Order1:=xlAscending, Header:=xlNo, _
                    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortTextAsNumbers
     Run time error 1004
    Subtotal method of range class failed           .Range("A4:CM4" & IngLastRow).Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, _
                    8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 83, 85, 86, 87, 88, 89, 90, 91), _
                    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    
                .Rows("4:4").AutoFilter
                .Range("T5:T" & IngLastRow).AutoFilter Field:=20, Criteria1:="1"
                
                With wsEachSheet
                    .Range("AN:AP,AR:AT,AW:BA").Interior.ColorIndex = 34
                    .Range("AN:AP,AR:AT,AW:BA").Pattern = xlSolid
                    .Range ("BC:BE,BV:BV,BX:BZ,CC:CD,CG:CK,CM:CM") Compile Error Invalid use of property
    
                    .FormatConditions.Delete
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="0"
                    .FormatConditions(1).Font.ColorIndex = xlAutomatic
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="0"
                    .FormatConditions(2).Font.ColorIndex = 5
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="0"
                    .FormatConditions(3).Font.ColorIndex = 3
                         
                With Columns("BF:BF")
                FormatConditions.Delete
                FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
                Formula1:="=$BH5"
                FormatConditions(1).Font.ColorIndex = 10
                FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                Formula1:="=$BH5"
                FormatConditions(2).Font.ColorIndex = 9
                Columns ("D:AL") Compile Error Invalid use of Property
    
                .EntireColumn.Hidden = True
                End With
                End With
            End With
        Next
        
        End Sub//

  7. #17
    MrExcel MVP Zack Barresse's Avatar
    Join Date
    Dec 2003
    Location
    Oregon, USA
    Posts
    10,281

    Default Re: sort sheets using macro ?

    This part...

    Code:
                With Columns("BF:BF")
                FormatConditions.Delete
                FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
                Formula1:="=$BH5"
                FormatConditions(1).Font.ColorIndex = 10
                FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                Formula1:="=$BH5"
                FormatConditions(2).Font.ColorIndex = 9
                Columns ("D:AL") Compile Error Invalid use of Property
    
                .EntireColumn.Hidden = True
                End With
    Is not being referenced by the With statement, because you have no preceding periods. You would need a preceding period before each "Columns" and "FormatConditions" because they are With the data range specified. If you don't it will be referencing the active worksheet (whatever one is selected).

    Also, your error line should be utilizing the Range object, not a Columns object, which is expecting a numerical value. You also need to do something with it. I'm assuming you're wanting to hide it, utilizing the next line of code, so combine them.

    You would need to replace that code with something like this...
    Code:
                With .Range("BF:BF")
                    .FormatConditions.Delete
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=$BH5"
                    .FormatConditions(1).Font.ColorIndex = 10
                    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=$BH5"
                    .FormatConditions(2).Font.ColorIndex = 9
                    .Range("D:AL").EntireColumn.Hidden = True            
                End With
    Regards,
    Zack Barresse
    Excel & Access blog
    All Excel Functions
    Training
    (If you would like comments in any code, please say so.)

  8. #18
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Thank you again for your help.
    I will adjust the code this afternoon and try it again.
    Still cant get the Subtotal part to work though.
    Also how do I copy the code to a different sheet I have several untouched versions of the worksheet for testing and dont know how to attach this code to them, are there any problems with doing this, there are also parts of the macro that would work for some of the other sheets we use.
    Hopefully I can get this to work soon

    Thanks
    Rick

  9. #19
    MrExcel MVP Zack Barresse's Avatar
    Join Date
    Dec 2003
    Location
    Oregon, USA
    Posts
    10,281

    Default Re: sort sheets using macro ?

    You're getting the error message because there is no sheet named "2" in the active workbook. Can you post a small subset of your data? If not, can you describe in detail your data structure? Include headers, data row start, etc. It's difficult to help you when we don't know anything about your data.
    Regards,
    Zack Barresse
    Excel & Access blog
    All Excel Functions
    Training
    (If you would like comments in any code, please say so.)

  10. #20
    Board Regular
    Join Date
    Oct 2007
    Location
    ecosse
    Posts
    84

    Default Re: sort sheets using macro ?

    Hi

    here is a small portion of one sheet, hope this helps

    Excel 2003
    ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKAL
    1SE Planned Maint As of RightsPeriodApril  2007 To Date
    2SEExternal NettTotalPlant &Sub - DirectNett GrossGross Gross OriginalSEExternal TotalPlant &Sub - DirectGross
    3WSIDSalesSalesPFFSalesContributWIPOIPSalesLabourTChargeHaulageMaterialsContractorsCosts Marginafter AdjMarginValueSalesSalesPFFContributWIPOIPSalesLabourTChargeHaulageMaterialsContractorsCosts Margin
    4Actual
    5BADDEBT BADDEBT0000000-443-4430-443
    6DTCP INCOME DTCP INCOME00000001414014
    7LUMP SUM Lump Sum0000000404,532404,5320404,532
    8LUMP SUM INCOM  000000020,4711,65022,120022,120
    9LUMPSUM Lump Sum0000000105,778105,7780105,778
    10RELEASE  00000000-18,500-18,50018,500
    1213SE78671 A720 Lasswade EB off slip patching TBA6161610616161111,5262,742-11914,14813,25113,251897
    1214SE78673 A1 Wallyford Southbound Off Slip  TBA Machine P000000042,1049,293-26451,13442,12142,1219,013
    1215SE78694 A1 Heughhead Farm  Site Investigation00000002,972401-7072,66633838531,1941,472
    1216SE78695 A1 Heughhead Farm  Design structural inlay000000070695-424378258258120
    1217SE78706 A720 Dreghorn WB Off Slip TBA Machine patching000000030,3306,519-74236,10730,57830,5785,529
    1218SE78766 A1 Spott Roundabout to Little Pinkerton SB  Struc0000000135,58022,293278158,151107,863107,86350,288
    1219SE78767 A720 Old Craighall to Sheriffhall Mains WB  TBA0000000164,27730,6603,549198,486157,979157,97940,507
    1220SE78786 A702 Backsheils to Candymill  TBA 50mm Inlay 1000000073,91519,028-1,59691,34777,39077,39013,957
    1221SE78842 A7 Hawick to Selkirk Road stud upgrade000000013,4462,75316,19915,71615,716483
    1222SE78843 A1 Cove  105 45mm Inlay0000000155,63144,311-7,490192,45252164,414164,46627,986
    1223SE78866 Patching flexible roads  Reactive Category 1 and00000002,0842,0842,0332,03351
    1224SE78887 A702 Wandel  Coring investigation00000001,992271-3791,88411535196381,247
    1225SE78888 A7 Eweslees Farm Pavement Investigation00000001,693230-3401,5841182528648936
    1226SE78891 Design A68 Sorrowlessfield Resurfacing000000020829-125112565656
    1227SE78892 A68 Sorrowlessfield Site Investigation00000001,455198-3551,298126474600698
    1228SE78927 A68 Jedburgh 120mm Inlay 320m2 100mm patching16,18316,18316,18315,78815,7883953953951177,78029,435-17,298189,917165,230165,23024,687
    1229SE78929 Design A702 Wandel  Structural inlay000000033247-199179909089
    1230SE78930 Design A7 Eweslees Farm Inlay000000027238-163147797967
    1231SE78950 A1 Redhall and Ayton Junctions Red Surfacing and R000000014,3371,91910716,36316,66816,668-305
    1232SE78973 A6091 Borders General Hospital Right Turn Prohibit0000000480958584758758-174
    1233SE79002 Road markings and reflector studs Reactive Categ641134774-389385605605170170-2191641134-389385605605-219
    1234SE79183 A702 Hillend    TBA  100mm inlay  320m2 100mm patc2,419302,449-82,4412,2122,2122372372301140,07135,137-3,523171,685151,968151,96819,717
    1235SE79295 Road markings and reflector studs Reactive Categ25353306-48258493493-187-187-235125353-48258493493-235
    1236SE79296 Road markings and reflector studs Reactive Categ8381751,013-10690790790710610601838175-1069079079070
    1237SE79333 A702 Abington Roundabouts Inlay TBA87,82015,744103,565-1,545102,02083,45283,45220,11320,11318,568187,82015,744-1,545102,02083,45283,45218,568
    1238SE79380 A720 Gilmerton EB off slip patching design51974593-312281141141452452140151974-312281141141140
    1239SE79383 Road markings and reflector studs Reactive Categ531164-1153250250-186-186-19715311-1153250250-197
    1240SE80107 BEAR has identified outstanding works from their s00000001,3001,30001,300
    1241SE90070 Installation of 10 New Gullies00000002,265594312,7553,4941,2607175,471-2,715
    1242SE90131 M8 Deans Road Bridge Design for bridge deck joints00000002,579365-1,547-2,579-1,1826363-1,245
    1243SE90174 Surface treatment00000000171171-171
    1244SE90175 not known00000000137137-137
    1245SE90176 Design of surface treatment for A68 Glendouglas00000000171171-171
    1246SE90177 Surface treatment00000000206206-206
    1247SE90205 Design for M9 Breastmill to J1A carriageway recons00000002,53678-1,5221,092943943149
    1248SE90246 Construction of hatching   ladder markings between000000012,23038612,6167318,44818,521-5,904
    1249SE90317 M8 Junction sA WB exit slip  Construction of remed00000007,1694153977,9819,5699,569-1,588
    1250SE90326 A80 Old Inns to Castlecary  Temporary Widening Rei00000001921920192
    1251SE90359 A720 Dreghorn carriageway reprofiling000000075,5993,840-3,22476,2161,44078,00079,440-3,224
    1252SE90374  A80 Old Inns to Castlecary   Temporary Widening0000000-1,7211,7210000
    1253SE90395 A68 Huntsford Bends   Construction of remedial Mea000000037,4741,4841,9125,22546,0952,051881,70233,94711,17448,962-2,867
    1254SE90399 A1 Houndwood000000031,73331,73331,73331,7330
    1255SE90416 A1 Spott Road Roundabout  Approaches to roundabout000000069,7805,080-21474,64525669,73169,9874,658
    1256SE90417 A1 Thistly Cross Roundabout  NB and SB Approaches000000058,0504,21180863,0692161,70461,7251,344
    1257SE90444 A68 Bends north of Earlston  Construction of AIP000000013,8981,268-39914,76853115,38215,913-1,145
    1258SE90623 A7 Opposite access to Branxholm Park Construction000000007,3417,341-7,341
    1259SE90631 M9 Junction 10 Craigforth  slip roads accident re000000089,76711,021-108100,6805116,762116,767-16,086
    1260SE906331  00000000-14,569-14,56914,569
    1261SE90666 A702 Crosshouses to Easter Howgate Resurfacing Des28741328-1721551031032252255318,350818-5,0104,1583,0063,0061,153
    1262SE90691 A7 Commercial Road Hawick  PUFFIN Crossing Includ0000000-1,313331-9820-982
    1263SE90692 A7 Commercial Road Hawick PUFFIN Crossing Civils00000006,7308,0002,48131117,52296515213112,95514,2033,319
    1264SE90741 A7 Opposite access to Branxholm Park Environmental00000001,084101124-1,0842260226
    1265SE90748 M9 Cambusbarron Boundary Fencing Design00000003,032293-1,8191,5061,0321,032474
    1266SE90769 A68 North of Pathhead Lining Signing and Road Stud000000014,2252,0062,36718,5981,11414,67315,7872,811

    4






    so this sheet has 1337 rows of data

    it is in 3 sections A-AL BASE DATA
    AN-BV is input section ie copy of D-AL where data in columns an-ap ar-at aw-ba can be changed the changes are reflected in bh-bm and bp-bt as these are cum figures
    third section bx-cm is current movement from base d-q

    as there are many rows with no data in current period d-q the sheets need sorted for current data re formula in col w
    they then need sorted by look up value in col c
    then subtotalled by code in col c
    and a value needs input from lokup in col v

    there are a number of blank columns am,av,bg,bo,bw,cf
    purelyto split sections of the sheet up.

    Does this help any

    Thanks
    Rick

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com