+ Reply to Thread
Results 1 to 6 of 6

Active X (Macro) to Sort data range to included added rows

Hybrid View

  1. #1
    Registered User
    Join Date
    07-14-2012
    Location
    Lincolnshire
    MS-Off Ver
    Excel 2010
    Posts
    5

    Active X (Macro) to Sort data range to included added rows

    Hi,

    I'm fairly new to macros and the like in excel so bare with me.
    I have a sheet which I am using to track spending and have a macro/Active X button to sort the data at the end. The trouble is that as I have added new rows they are not included in the range and have to change that each time. Is there a way to get the macro to check the range by looking for the cells that have data in a specific column, say the date column?

    Basically the macro selects the rows and columns with data in and then sorts it by the date range descending.

    Heres the macro code
    Private Sub CommandButton1_Click()
    '
    ' Kitpartsspending Macro
    '
    
    '
        Range("B3:F33").Select
        ActiveWorkbook.Worksheets("Kitparts Spending").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Kitparts Spending").Sort.SortFields.Add Key:=Range _
            ("B3:B33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Kitparts Spending").Sort
            .SetRange Range("B2:F33")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWindow.SmallScroll Down:=15
        Range("E35").Select
    
    End Sub
    Any help would be greatly appreciated

    Thanks in advance

  2. #2
    Forum Contributor
    Join Date
    06-07-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: Active X (Macro) to Sort data range to included added rows

    Try

    Private Sub CommandButton1_Click()
    '
    ' Kitpartsspending Macro
    '
    
    '
    Dim LR As Long
    LR = Worksheets("Kitparts Spending").Range("B" & Rows.Count).End(xlUp).Row
        Range("B3:F" & LR).Select
        ActiveWorkbook.Worksheets("Kitparts Spending").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Kitparts Spending").Sort.SortFields.Add Key:=Range _
            ("B3:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Kitparts Spending").Sort
            .SetRange Range("B2:F" & LR)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    End Sub

  3. #3
    Registered User
    Join Date
    07-14-2012
    Location
    Lincolnshire
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Active X (Macro) to Sort data range to included added rows

    Fantastic,
    That works a treat thank you...I just need to read over it now and see what it is you've done to make it work

    Is there a way to make it select the cell just above the total after sorting too, I know how to do this if I record as a macro but the cell selected again obviously changes when new rows are added.

    Thanks for your help

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Active X (Macro) to Sort data range to included added rows

    Another way:

    Private Sub CommandButton1_Click()
        Dim wks         As Worksheet
        Dim iRow        As Long
    
        Set wks = Worksheets("Kitparts Spending")
    
        With wks
            iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
            .Range("B3", .Cells(iRow, "F")).Sort _
                    Key1:=.Range("B3"), Order1:=xlAscending, _
                    Header:=xlYes, _
                    MatchCase:=False, _
                    Orientation:=xlTopToBottom
            Application.Goto .Cells(iRow - 1, "B")
        End With
    End Sub
    Last edited by shg; 07-14-2012 at 01:12 PM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    07-14-2012
    Location
    Lincolnshire
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Active X (Macro) to Sort data range to included added rows

    Quote Originally Posted by shg View Post
    Another way:

    Private Sub CommandButton1_Click()
        Dim wks         As Worksheet
        Dim iRow        As Long
    
        Set wks = Worksheets("Kitparts Spending")
    
        With wks
            iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
            .Range("B3", .Cells(iRow, "F")).Sort _
                    Key1:=.Range("B3"), Order1:=xlAscending, _
                    Header:=xlYes, _
                    MatchCase:=False, _
                    Orientation:=xlTopToBottom
            Application.Goto .Cells(iRow - 1, "B")
        End With
    End Sub
    Err, for some reason when I try this version and put in a date before the row at the very top it inserts it after the top line. Any idea why that is, otherwise it seems to work?

  6. #6
    Registered User
    Join Date
    07-14-2012
    Location
    Lincolnshire
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Active X (Macro) to Sort data range to included added rows

    Quote Originally Posted by tims31 View Post
    Err, for some reason when I try this version and put in a date before the row at the very top it inserts it after the top line. Any idea why that is, otherwise it seems to work?
    Ok, I changed the range selection to B2 and it works ok now..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