+ Reply to Thread
Results 1 to 13 of 13

Get Sheet Names - Remove Sort from this code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Get Sheet Names - Remove Sort from this code

    This code works well to get sheet names form a closed workbook. But can someone tell me how to remove the sorting Alphabetical form this code? Its a little too complex for me I dont know how to remove the sort.

    Option Explicit
    
    Public Sub DemoGetSheetNames()
    
        Dim lNumEntries As Long
        Dim szFullName As String
        Dim szFileSpec As String
        Dim aszSheetList() As String
        
        Columns("A:A").Select
        Selection.ClearContents
    
        szFileSpec = "Excel Files (*.xl*),*.xl*"
        
        szFullName = CStr(Application.GetOpenFilename(szFileSpec, , "Select an Excel File"))
        
        ''' Continue if the user did not cancel the dialog.
        If szFullName <> CStr(False) Then
            GetSheetNames szFullName, aszSheetList()
            lNumEntries = UBound(aszSheetList) - LBound(aszSheetList) + 1
            Sheet1.Range("A1").Resize(lNumEntries).Value = Application.WorksheetFunction.Transpose(aszSheetList())
            Sheet1.Range("A1").EntireColumn.AutoFit
        End If
    
    End Sub
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' Comments:   Returns a string array containing the list of worksheets in
    '''             the specified workbook.
    '''             NOTE: Requires references to the following object library:
    '''             * Microsoft ActiveX Data Objects 2.5 Library (or higher version)
    '''
    ''' Arguments:  szFullName      [in] The full path and filename of the workbook
    '''                             whose worksheet list you want to query.
    '''             aszSheetList()  [out] Will be loaded with a list of worksheets
    '''                             in the workbook specified by szFullName.
    '''
    ''' Date        Developer       Action
    ''' --------------------------------------------------------------------------
    ''' 05/13/05    Rob Bovey       Created
    '''
    Private Sub GetSheetNames(ByRef szFullName As String, ByRef aszSheetList() As String)
    
        Dim bIsWorksheet As Boolean
        Dim objConnection As ADODB.Connection
        Dim rsData As ADODB.Recordset
        Dim lIndex As Long
        Dim szConnect As String
        Dim szSheetName As String
    
        Erase aszSheetList()
        If Application.Version < 12 Then
            szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
        Else
            szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
        End If
    
        Set objConnection = New ADODB.Connection
        objConnection.Open szConnect
        Set rsData = objConnection.OpenSchema(adSchemaTables)
    
        Do While Not rsData.EOF
            bIsWorksheet = False
            szSheetName = rsData.Fields("TABLE_NAME").Value
            If Right$(szSheetName, 1) = "$" Then
                ''' This is a simple sheet name. Remove the trailing "$" and continue.
                szSheetName = Left$(szSheetName, Len(szSheetName) - 1)
                bIsWorksheet = True
            ElseIf Right$(szSheetName, 2) = "$'" Then
                ''' This is a sheet name with spaces and/or special characters.
                ''' Remove the right "&'" characters.
                szSheetName = Left$(szSheetName, Len(szSheetName) - 2)
                ''' Remove the left single quote character.
                szSheetName = Right$(szSheetName, Len(szSheetName) - 1)
                bIsWorksheet = True
            End If
            If bIsWorksheet Then
                ''' Embedded single quotes in the sheet name will be doubled up.
                ''' Replace any doubled single quotes with one single quote.
                szSheetName = Replace$(szSheetName, "''", "'")
                ''' Load the processed sheet name into the array.
                ReDim Preserve aszSheetList(0 To lIndex)
                aszSheetList(lIndex) = szSheetName
                lIndex = lIndex + 1
            End If
            rsData.MoveNext
        Loop
    
        rsData.Close
        Set rsData = Nothing
        objConnection.Close
        Set objConnection = Nothing
    
    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    Hi, I would get rid of the entire ActiveX etc and just do it this way

    Option Explicit
    
    Public Sub DemoGetSheetNames()
    
        Dim lNumEntries As Long
        Dim szFullName As String
        Dim szFileSpec As String
        Dim aszSheetList() As String
        Dim currWB  As Workbook
        Dim currWS  As Worksheet
        Dim tmpWB   As Workbook
        Dim ws      As Worksheet
        
        Set currWB = ThisWorkbook
        Set currWS = currWB.ActiveSheet
        currWS.Columns("A:A").ClearContents
    
        szFileSpec = "Excel Files (*.xl*),*.xl*"
        
        szFullName = CStr(Application.GetOpenFilename(szFileSpec, , "Select an Excel File"))
        
        ''' Continue if the user did not cancel the dialog.
        If szFullName <> CStr(False) Then
            Application.ScreenUpdating = False
            On Error Resume Next
            Set tmpWB = Workbooks.Open(szFullName, UpdateLinks:=False, ReadOnly:=True)
            If Not tmpWB Is Nothing Then
                Err.Clear
                For Each ws In tmpWB.Worksheets
                    currWS.Range("A" & currWS.Range("A" & Rows.Count).End(xlUp).Row).Offset(1, 0).Value = ws.Name
                Next ws
                tmpWB.Close False
            End If
        End If
        On Error GoTo 0
        Application.ScreenUpdating = True
        currWB.Activate
    End Sub
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    I have been trying to get this to work and having issues. it clears the existing A Column but doesn't add back the sheet names to the file I open. Before it didn't actually open the file i am pulling the sheet names from (which is how I wanted it), and now its trying to open it (because its activating a macro within the file I am selecting). I have to change the column to B not sure of that screwed it up?

    This is the original code with B being the destination

    Option Explicit
    
    Public Sub DemoGetSheetNames()
    
        Dim lNumEntries As Long
        Dim szFullName As String
        Dim szFileSpec As String
        Dim aszSheetList() As String
        
        Columns("B:B").Select
        Selection.ClearContents
    
        szFileSpec = "Excel Files (*.xl*),*.xl*"
        
        szFullName = CStr(Application.GetOpenFilename(szFileSpec, , "Select an Excel File"))
        
        ''' Continue if the user did not cancel the dialog.
        If szFullName <> CStr(False) Then
            GetSheetNames szFullName, aszSheetList()
            lNumEntries = UBound(aszSheetList) - LBound(aszSheetList) + 1
            Sheet1.Range("B3").Resize(lNumEntries).Value = Application.WorksheetFunction.Transpose(aszSheetList())
            Sheet1.Range("B3").EntireColumn.AutoFit
        End If
    
    End Sub
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' Comments:   Returns a string array containing the list of worksheets in
    '''             the specified workbook.
    '''             NOTE: Requires references to the following object library:
    '''             * Microsoft ActiveX Data Objects 2.5 Library (or higher version)
    '''
    ''' Arguments:  szFullName      [in] The full path and filename of the workbook
    '''                             whose worksheet list you want to query.
    '''             aszSheetList()  [out] Will be loaded with a list of worksheets
    '''                             in the workbook specified by szFullName.
    '''
    ''' Date        Developer       Action
    ''' --------------------------------------------------------------------------
    ''' 05/13/05    Rob Bovey       Created
    '''
    Private Sub GetSheetNames(ByRef szFullName As String, ByRef aszSheetList() As String)
    
        Dim bIsWorksheet As Boolean
        Dim objConnection As ADODB.Connection
        Dim rsData As ADODB.Recordset
        Dim lIndex As Long
        Dim szConnect As String
        Dim szSheetName As String
    
        Erase aszSheetList()
        If Application.Version < 12 Then
            szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
        Else
            szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
        End If
    
        Set objConnection = New ADODB.Connection
        objConnection.Open szConnect
        Set rsData = objConnection.OpenSchema(adSchemaTables)
    
        Do While Not rsData.EOF
            bIsWorksheet = False
            szSheetName = rsData.Fields("TABLE_NAME").Value
            If Right$(szSheetName, 1) = "$" Then
                ''' This is a simple sheet name. Remove the trailing "$" and continue.
                szSheetName = Left$(szSheetName, Len(szSheetName) - 1)
                bIsWorksheet = True
            ElseIf Right$(szSheetName, 2) = "$'" Then
                ''' This is a sheet name with spaces and/or special characters.
                ''' Remove the right "&'" characters.
                szSheetName = Left$(szSheetName, Len(szSheetName) - 2)
                ''' Remove the left single quote character.
                szSheetName = Right$(szSheetName, Len(szSheetName) - 1)
                bIsWorksheet = True
            End If
            If bIsWorksheet Then
                ''' Embedded single quotes in the sheet name will be doubled up.
                ''' Replace any doubled single quotes with one single quote.
                szSheetName = Replace$(szSheetName, "''", "'")
                ''' Load the processed sheet name into the array.
                ReDim Preserve aszSheetList(0 To lIndex)
                aszSheetList(lIndex) = szSheetName
                lIndex = lIndex + 1
            End If
            rsData.MoveNext
        Loop
    
        rsData.Close
        Set rsData = Nothing
        objConnection.Close
        Set objConnection = Nothing
    
    End Sub

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    The code you have just attached works fine here so I don't get the issue.
    Yes, the only thing is the sorting which remains.
    The code I posted without the ActiveX works fine too except that I added the enable events False to avoid macro's from being executed
    Public Sub DemoGetSheetNames02()
    
        Dim lNumEntries As Long
        Dim szFullName As String
        Dim szFileSpec As String
        Dim aszSheetList() As String
        Dim currWB  As Workbook
        Dim currWS  As Worksheet
        Dim tmpWB   As Workbook
        Dim ws      As Worksheet
        
        Set currWB = ThisWorkbook
        Set currWS = currWB.ActiveSheet
        currWS.Columns("B:B").ClearContents
    
        szFileSpec = "Excel Files (*.xl*),*.xl*"
        
        szFullName = CStr(Application.GetOpenFilename(szFileSpec, , "Select an Excel File"))
        
        ''' Continue if the user did not cancel the dialog.
        If szFullName <> CStr(False) Then
            With Application
                .EnableEvents = False
                .ScreenUpdating = False
            End With
            
            On Error Resume Next
            Set tmpWB = Workbooks.Open(szFullName, UpdateLinks:=False, ReadOnly:=True)
            If Not tmpWB Is Nothing Then
                Err.Clear
                For Each ws In tmpWB.Worksheets
                    currWS.Range("B" & currWS.Range("B" & Rows.Count).End(xlUp).Row).Offset(1, 0).Value = ws.Name
                Next ws
                tmpWB.Close False
            End If
        End If
        On Error GoTo 0
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        currWB.Activate
    End Sub

  5. #5
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    It doenst work with the XLSB file I'm trying to do it with. But my original issue is the sorting, I didnt want it to sort A-Z. Did your code fix this?

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    Here are two files with the same code, one xlsm and one xlsb
    Attached Files Attached Files

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    The code I posted works with any Excel file that will permit macros.
    If you say it doesn't work, WHAT doesn't work? Be a little more specific

  8. #8
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    That was absolutely genius!! Thank you so much!

  9. #9
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    How can I get the sheet name list to start in row 3 instead of row 2?

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    I don't want to come across as be-learning but when you already have vba code and use it, I would at least expect some basic idea of how VBA works.
    I would suggest you play along with the code and test, trial and error, that's the way it works and a lot of patience and time.
    For now this is what you have to do

    Modify this section of the code (my version) insert the row marked in red

        currWS.Columns("B:B").ClearContents
        currWS.Cells(2, 2).Value = "Hello"
        szFileSpec = "Excel Files (*.xl*),*.xl*"
    Place any text you want instead of Hello but it's to give you the idea.
    Since the entire column is cleared of contents you have to re-insert a value to be used as header, so you can do that on any row

  11. #11
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    I took an online course a few years ago. If you know any good ones let me know (seriously no sarcasm). I really appreciate your help!

  12. #12
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Get Sheet Names - Remove Sort from this code

    I never got the change for taking any courses in VBA, just practice and lots of time.
    I was lucky that my job permitted this and I just went for it.
    Some links, not all really tried out but some things I did pick up there
    https://excelvbaisfun.com/#Intro
    And the next one is really great
    http://www.cpearson.com/Excel/Topic.aspx

    You can also check out Ron de Bruin, good one too
    Happy coding

  13. #13
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Get Sheet Names - Remove Sort from this code

    Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Trying to remove duplicate names from a look up code to count
    By MR22 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-22-2018, 04:46 PM
  2. [SOLVED] How do i remove names from sheet if it appears on another sheet
    By dougers1 in forum Excel General
    Replies: 3
    Last Post: 05-15-2016, 12:57 PM
  3. Replies: 28
    Last Post: 06-07-2015, 04:20 PM
  4. [SOLVED] VB Code to sort Names Alphabettically
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-05-2015, 11:41 AM
  5. Replies: 1
    Last Post: 10-06-2014, 09:44 AM
  6. VBA code to Copy, remove duplicate and sort
    By GEMINI528 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2013, 04:12 PM
  7. [SOLVED] Remove names from comments with vba code
    By Harribone in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2013, 03:59 PM

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