+ Reply to Thread
Results 1 to 12 of 12

Sorting data in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Sorting data in VBA

    Hi All,

    I am encountering numerous problems compiling a macro to sort some pretty messy data. Hoping some of you could take a look and offer some suggestions....

    The Data I need sorting is shown in the 'DATARAW' worksheet, this comes in every day and shows positions for each traded period. Usually 48 periods which I have highlighted although the data download could somtimes consist of 46 or 50 periods.

    Sheet 1 (DATA RAW) contains data which I download daily.

    Sheet 2 (Data Sort) is how I need it to look after sorted (I only need the data in DATARAW replacing, not another worksheet creating).

    So for example:

    Where column A = SP8 and the adjacent column B = 1 I would need:

    All values which = ECN to concatenate the value in B to the reference in column B directly adjacent to the value next to it, until the folowing rows are presented:

    So for instance the first value reported would be 131964, second 131979 etc... until the following text is shown in the rows below

    IDD
    CAT
    ACE
    ACT
    ACE
    ACT


    When these appear I need the entire rows to be deleted.

    Then basically the same for the next period where SP8 appears in column A and 2 appears in Column B.

    also, the final result should contain no formulas so it can be referenced by lookups etc... also, all numeric colums should be stored as number so excel doesn't get confused.

    This will basically repeat itself until the end of the file showing the same split as aove but with ZZZ as a footer. Which I also need to delete. :-S

    IDD
    CAT
    ACE
    ACT
    ACE
    ACT
    ZZZ

    Rows containing the values below also need deleting.

    AAA
    NOH
    DSI
    AED


    I've used colours in the attachment but the actual file doesn't have any.

    Am sure this would be a simple macro for many of you but I am totally stumped, any help would be very much apprieciated.
    Attached Files Attached Files
    Last edited by Dr Mambo; 03-19-2009 at 05:29 AM. Reason: Missing Information

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    re: Sorting data in VBA

    Hi

    See how this goes

    Sub aaa()
      Sheets("Data Raw").Select
      Range("A1:L1").Value = Array("H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12")
      
      Rows("2:4").Delete
      arr = Array("IDD", "CAT", "ACE", "ACT", "ZZZ")
      
      For i = LBound(arr) To UBound(arr)
        Range("A:L").AutoFilter field:=1, Criteria1:=arr(i)
        Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      Next i
      ActiveSheet.ShowAllData
      ActiveSheet.AutoFilterMode = False
      
      For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) = "SP8" Then
          holder = Cells(i, 2).Value
        Else
          Cells(i, 1).Value = holder & Cells(i, 2).Value
          Cells(i, 3).Value = holder
        End If
      
      Next i
      
      Range("A:L").AutoFilter field:=1, Criteria1:="SP8"
      Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      ActiveSheet.AutoFilterMode = False
      Rows("1:1").Delete
      Range("A:L").Sort key1:=Range("A1"), order1:=xlAscending
      
      
    End Sub
    rylo

  3. #3
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    Hi,

    Thanks... that works brilliantly, however when I copy the macro to my Existing Project (containing other pages with macros) I am encountering a compiler error, it just wont let me run it in a different spreadsheet. Any Ideas?

    Ta

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Sorting data in VBA

    Hi

    1) What is the error
    2) Do you have an option explicit? If so, then all the variables in my macro will have to be defined.
    3) Can you replicate the problem in an example file. If so, please attach..


    rylo

  5. #5
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    The error seems to relate to this section in the code and stops here.

    arr = Array("IDD", "CAT", "ACE", "ACT", "ZZZ")

    I have the working example attached as 'Working Macro' The content is exactly the same and it works.

    This consists of 3 worksheets

    1) Go Button (button running the macro)
    2) Data to copy (nothing attached to this it's just got a copy of the raw data file to sort)
    3) DataCont

    Pressing the SORT EOD button runs the macro fine.


    The worksheet I'm trying to add it to is 'EODReportCheckv1.1WIP (most of the buttons won’t work as they are relative to file locations so the test buttons are:

    Copy EOD (gets a copy of the file data and places it into the DataCont worksheet).

    Sort EOD (this contains a carbon copy of the macro in the Working macro workbook). Clicking the macro button produces the error mentioned above... even more bizarrely, if I open the Working Macro workbook and assign that macro to the button in the 'EODReportCheckv1.1WIP workbook against the Copy EOD button and then close the Working Macro Workbook down. The working macro workbook will open and the macro will run perfectly in the 'EODReportCheckv1.1WIP workbook.

    Again, thanks for your help and hope to hear from you soon!
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Sorting data in VBA

    Hi

    I opened your EOD file, and pressed CopyCOD then SortCOD and both ran fine. No errors just went straight through...

    Try just having that opened with no other workbook and see if that helps.

    rylo

  7. #7
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    Nope still doesn't work on any of my machines, I do have an option explict on quite a few of my macros (may not have included them) I need to define the varibles in the header but have been playing around and still havent got them. Could anyone define these and show me?

    Ta

  8. #8
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    PS I know I'm being a pain :-S

  9. #9
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    Hi,

    Still Having issues, have copied the names from Rylo's macro and changed for the approrriate worksheets etc as below:

    Sub EODSort()




    Sheets("DataSort").Select
    Range("A1:L1").Value = Array("H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12")

    Rows("2:4").Delete
    arr = Array("IDD", "CAT", "ACE", "ACT", "ZZZ")

    For i = LBound(arr) To UBound(arr)
    Range("A:L").AutoFilter Field:=1, Criteria1:=arr(i)
    Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Next i
    ActiveSheet.ShowAllData
    ActiveSheet.AutoFilterMode = False

    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    If Cells(i, 1) = "SP8" Then
    holder = Cells(i, 2).Value
    Else
    Cells(i, 1).Value = holder & Cells(i, 2).Value
    Cells(i, 3).Value = holder
    End If

    Next i

    Range("A:L").AutoFilter Field:=1, Criteria1:="SP8"
    Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ActiveSheet.AutoFilterMode = False
    Rows("1:1").Delete
    Range("A:L").Sort Key1:=Range("A1"), Order1:=xlAscending


    End Sub


    As my current workbook contains an option expilct with named variables (these are from recorded macros), it still wont work with my project.

    All I literally need is the variables assigning for the abov project and I'm sure that would make it work in my current workbook. Just havent got a clue. Have tried numerous times and it just kills the macro.

  10. #10
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    Have tried with the following variables inserted

    Sub EODSort()

    Dim arr As String
    Dim i As Integer
    Dim Holder As Integer

    But it kills the macro at LBound, have no experience with defining variables so am stabbing in the dark here.

    ta

  11. #11
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Sorting data in VBA

    Hi

    Try changing
    Dim arr As String
    to
    Dim arr As variant

    rylo

  12. #12
    Registered User
    Join Date
    03-14-2009
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Sorting data in VBA

    Rylo, you're a gentleman and a Scholar...

    Works fine now :-)


    Many Thanks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1