+ Reply to Thread
Results 1 to 6 of 6

Auto sort excel table in VB

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2012
    Location
    CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Auto sort excel table in VB

    I'm trying to auto sort a table in excel. I want to have the table in "Summary" sheet automatically sort when a cell is changed in column A. I also want the numbers in the sort to be sorted last so that the text is first in the list.

    I've attached the file I'm working on. I copied a macro to auto sort in VB but it does not work right. The cells in column A refers to another cell. When I change the value in the other cells the table will not sort. I also get an error message when i open the file now.

    Can anyone help?
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Auto sort excel table in VB

    I don't know how you change the value in Protected sheet though.

    To Summary sheet module
    
    Private Sub Worksheet_Calculate()
        mySort
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        mySort
    End Sub
    
    Private Sub mySort()
        Dim ws As Worksheet
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        For Each ws In Worksheets
            If IsNumeric(ws.Name) Then
                With ws.Range("c3")
                    If Right$(.Formula, 2) <> "+0" Then
                        .Formula = .Formula & "+0"
                    End If
                End With
            End If
        Next
        With Me
            .Unprotect
            With .Range("a2").CurrentRegion
                .Sort key1:=.Cells(1), order1:=2, Header:=xlYes
                With .Columns(1).SpecialCells(-4123, 1).EntireRow
                    .Sort key1:=.Cells(1), order1:=1, Header:=xlNo
                End With
                With .Columns(1).SpecialCells(-4123, 2).EntireRow
                    .Sort key1:=.Cells(1), order1:=1, Header:=xlNo
                End With
            End With
            .Protect
        End With
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    09-08-2012
    Location
    CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Auto sort excel table in VB

    I spoke too soon. The problem is when I change a Sheet name from a number to a Text I get the error "#value!". It looks to me the module it refers to cell "C3" as a number. So this adds a "+0" to my formula in the C3 cell.

    Any suggestions?

  4. #4
    Registered User
    Join Date
    09-08-2012
    Location
    CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Auto sort excel table in VB

    This works perfect! Thank you.

    The cell values in column A come from other sheets. This is why it's locked and protected and how the values are changed.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Auto sort excel table in VB

    Try replace following part in "mySort" sub procedure
        For Each ws In Worksheets
            If IsNumeric(ws.Name) Then
                With ws.Range("c3")
                    If Right$(.Formula, 2) <> "+0" Then
                        .Formula = .Formula & "+0"
                    End If
                End With
            End If
        Next
    with
        For Each ws In Worksheets
            With ws.Range("c3")
                If IsNumeric(.Parent.Name) Then
                    If Right$(.Formula, 2) <> "+0" Then
                        .Formula = .Formula & "+0"
                    End If
                Else
                    If Right$(.Formula, 2) = "+0" Then
                        .Formula = Left$(.Formula, Len(.Formula) - 2)
                    End If
                End If
            End With
        Next

  6. #6
    Registered User
    Join Date
    09-08-2012
    Location
    CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Auto sort excel table in VB

    That seems to work.

    Thank you.

+ 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