Results 1 to 15 of 15

Error in Code to Sort, Hide, and PDF Worksheets

Threaded View

  1. #1
    Registered User
    Join Date
    04-02-2014
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    8

    Error in Code to Sort, Hide, and PDF Worksheets

    Hi,

    I'm currently working with the following code to reorder my Excel worksheets based on a list (of certain worksheet names) in a specified range (in this case "WorksheetNames"). Once the worksheets in that range are sorted, the next function hides the remaining worksheets based on a similar list (in this case, the range "Hidden"). Finally, the code should PDF the newly reordered worksheets only.

    QUESTION: This code works (sorts the worksheets, and hides the others, but doesn't yet PDF), but is currently returning a "Select method of Worksheet class failed" error (I believe error 400?), but I'm not quite sure what the issue could be. I'm fairly new to VBA, and have pieced the code together from various sources, so any help in getting it to work properly would be greatly appreciated. Thanks!


    Sub test()
        Dim rng As Range, i As Long, msg As String
        On Error GoTo Errorcatch
        Set rng = Range("WorksheetNames")
        For i = rng.Count To 1 Step -1
            If rng(i).Value <> "" Then
                If IsSheetExists(rng(i).Value) Then
                    Sheets(rng(i).Value).Move after:=rng.Parent
                Else
                    msg = msg & vbLf & rng(i).Value
                End If
            End If
        Next
        rng.Parent.Select
        If Len(msg) Then MsgBox "Wrong sheet name" & msg
        Set rng = Nothing
        Exit Sub
    Errorcatch:
        MsgBox Err.Description
    End Sub
     
     
    Function IsSheetExists(txt As String) As Boolean
        On Error Resume Next
        For Each cell In Sheets("Appendices").Range("Hidden")
            ' Hide sheets
            Sheets(cell.Value).Visible = False
        Next cell
    End Function
    
    Function SaveFileToPDF()
        ActiveWorkbook.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                FileName:="C:\Compustat Data\Test.pdf", _
                OpenAfterPublish:=True
    End Function
    Last edited by mbhart; 04-04-2014 at 12:50 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help with VBA Code (Hide/Unhide Worksheets)
    By maryren in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2014, 12:43 PM
  2. Help me sort this code layout error! Missing inverted comma!
    By ropbasuel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2013, 12:31 AM
  3. [SOLVED] VBA code to sort data either within one large worksheet or across several worksheets
    By miss_georgina in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2012, 04:52 PM
  4. Excel 2007 Code to Hide/Protect Worksheets
    By mab in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-11-2010, 03:54 PM
  5. [SOLVED] Code to hide worksheets based on Active Directory login?
    By Joe in forum Excel General
    Replies: 0
    Last Post: 02-21-2006, 12:20 AM

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