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
Bookmarks