+ Reply to Thread
Results 1 to 3 of 3

Macro for arranging in descending order.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2010
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    1

    Macro for arranging in descending order.

    Hello All,

    I am new to this forum (a little over than a week). Frankly speaking, i haven't seen an MS Excel VBA forum so active with all experienced individuals. I would require your assistance in writing a macro and would appreciate any help on it.

    I have these three enclosed files which i update every week.Each of the three sheets include:

    Row 1: Red highlighted area is for a new transaction and date at which it occurred.
    Row 2: Description of the transaction (will always remain fixed)
    Row 3: Details of the transaction (could go up to a maximum number of 10-15)

    The challenge that i face is of arranging them in descending order (latest date transaction should appear first following the others) in a separate spreadsheet.

    Please help..Thank you...
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Macro for arranging in descending order.

    Welcome to the forum! How expereinced are you with Excel VBA? I've got an idea for how to do this, but I need to know where you are coming from.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro for arranging in descending order.

    Hello Taureankv,

    Welcome tot he Forum!

    I have added the following macro to the attached workbook. This will sort all the accounts in the open workbooks and copy the account information tables to the main workbook on "Sheet1".

    The macro assumes the following conditions:
    1) The layout of the actual files to be the same as the original.
    2) All worksheets that contain account information are assumed to be named "Sheet1".
    3) All account information data is separated by 1 blank line.
    Sub ListAccounts()
    
      Dim Account As String
      Dim Cell As Range
      Dim Data As Range
      Dim DSO As Object
      Dim MainWkb As Workbook
      Dim R As Long
      Dim Rng As Range
      Dim TD As String
      Dim Wkb As Workbook
      Dim Wks As Workbook
    
        Set MainWkb = ThisWorkbook
        Set DSO = CreateObject("Scripting.Dictionary")
        
          For Each Wkb In Workbooks
           If Wkb.Name <> MainWkb.Name Then
              R = 1
              With Wkb.Worksheets(1)
                While .Cells(R, "A").Address <> .Cells(R, "A").CurrentRegion.Address
                 'Get Account name and  transaction date
                  Account = .Cells(R, "A")
                  TD = Format(.Cells(R, "F"), "mm/dd/yyyy")
                 'Get the cells that make up the table
                  Set Data = .Cells(R, "A").CurrentRegion
                  DSO.Add TD & " " & Account, Data
                  R = R + .Cells(R, "A").CurrentRegion.Rows.Count + 1
                Wend
              End With
           End If
          Next Wkb
          
         'Sort the list of account names by date in descending order
          With MainWkb.Worksheets("Sheet2")
            Set Rng = .Range("A1").Resize(DSO.Count, 1)
            Rng.Clear
            Rng.Value = WorksheetFunction.Transpose(DSO.Keys)
            Rng.Sort Key1:=Rng.Cells(1, 1), Order1:=xlDescending, _
                     Header:=xlNo, Orientation:=xlTopToBottom
          End With
          
         'Copy account tables in descending order to "Sheet1" in the main workbook
          With MainWkb.Worksheets("Sheet1")
            R = 1
            For Each Cell In Rng
              DSO(Cell.Text).Copy
              .Cells(R, 1).PasteSpecial Paste:=xlPasteAll
              R = R + DSO(Cell.Text).Rows.Count + 1
            Next Cell
          End With
          
       'Free the object and memory used
        Set DSO = Nothing
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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