Results 1 to 1 of 1

Trying to define a range to include both rows and columns with

Threaded View

  1. #1
    Registered User
    Join Date
    05-10-2013
    Location
    Regina, Saskatchewan
    MS-Off Ver
    Excel 2010
    Posts
    1

    Trying to define a range to include both rows and columns with

    Hi,
    I have a number or different types of equipment as vehicles on an excel page. Ie Toyota 1993 spans columns A to E in row 1 and Honda 1995 spans columns F to J and so on. Under each vehicle in row 2 there are 5 sub headings: Date, location, comment, ect. New data is entered into this speadsheet by user input in a form. The form seaches for the vehicle in the A row and once it finds it, it goes to the bottom of the sheet and adds the information. Now I want each vehicle in the sheet to be sorted by the date from newest to oldest. Basically I want to select from A3 to E1048576 and sort this data by date (This would be all the information under Toyota), and then I want the macro to automatically offset itself by 5 columns so that it is now at F3 and to select from here to J1048576 (This would be all the information under the Honda) I recorded a macro to sort just one of these areas but I want it to be a variable cell range. Not quite sure how to do this. I've included my code below:
    Sheets("Archive").Select
    
    Dim i As Double, value As Double, ws As Worksheet, CellRange As Range
    
    Set ws = Worksheets("Archive")
    
    i = 0
    
    Do
    Range("A3").Select
    value = i * 5
    
    ActiveCell.offset(0, value).Select
    
    Set CellRange = ws.Range(Range(A3, A1048576), Range(E3, E1048576))
    
    Set CellRange = ws.Range(ActiveCell.Address, ActiveCell.offset(1048576, 4).Address)
    
    With ActiveWorkbook.Worksheets("Archive").Sort
            .SetRange Range("CellRange")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        ActiveWorkbook.Worksheets("Archive").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Archive").Sort.SortFields.Add Key:=Range("CellRange") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
    With ActiveWorkbook.Worksheets("Archive").Sort
            .SetRange Range("CellRange")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    i = i + 1
    Loop Until i > NumberOfVehicles 'Number of vehicles was previously defined
    Last edited by Leith Ross; 05-11-2013 at 04:50 AM. Reason: Added Code Tags

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