+ Reply to Thread
Results 1 to 2 of 2

Error 1004 Select method from worksheet class failed

  1. #1
    Registered User
    Join Date
    08-09-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2016
    Posts
    0

    Error 1004 Select method from worksheet class failed

    Hi,

    I am a new member who is learning VBA and have been using Excel for many years. I am an Accountant/Operations Research modeller who loves Excel.


    But, I am having trouble with Excel VBA code in a Table of Contents module. I get an error message "Select method from worksheet class failed."

    I would appreciate if someone could advise me how to eliminate the error in the following code:

    'This table of contents VBA code was created by Frank Wade on 31/10/2014:

    Sub TOC()
    ' Declaring variables:
    Dim wrk As Workbook
    Dim sht As Worksheet 'i.e. the Table of Contents sheet
    Dim shtTarget As Worksheet
    Dim rng As Range
    Dim intSheetNum As Integer
    Dim intNumRows As Integer
    Dim strSheetName As String
    Dim lngRow As Long
    Dim lngColumn As Long
    Dim varSubaddress As Variant
    Dim blnCreatTOC As Boolean
    Dim SheetName As String 'SheetName is a string used in the code to add the TOC hyperlink on each sheet

    On Error Resume Next

    Set wrk = ActiveWorkbook

    If wrk.ProtectStructure = True Then
    MsgBox "You must unprotect the workbook " & "before running this procedure."
    Else
    blnCreateTOC = True
    Set sht = wrk.Worksheets("Table of Contents")
    If Err = 0 Then
    If MsgBox("Table of Contents already exists." & " Delete it and create a new one?", _
    vbYesNo, "Sheet Exists") = vbYes Then

    On Error GoTo Err_Handler
    Application.DisplayAlerts = False
    sht.Delete
    Application.DisplayAlerts = True
    Else
    blnCreateTOC = False
    End If
    End If
    End If

    On Error GoTo Err_Handler
    If blnCreateTOC Then

    intNumRows = InputBox("How many blank rows should " & "separate links?", "Blank Rows", 1) 'i.e. the default is 1 row

    Application.ScreenUpdating = False
    Set sht = wrk.Worksheets.Add(wrk.Sheets(1))
    With sht
    .Name = "Table of Contents"
    .Columns(1).ColumnWidth = 2
    .Range("B2") = "TABLE OF CONTENTS (TOC)"
    .Range("B2").Font.Bold = True ' to bold the text font
    .Range("B2").Font.Color = RGB(255, 255, 255) ' white font colour
    .Range("B2").Interior.Color = RGB(0, 0, 0) ' black background colour
    .Range("B2:H2").MergeCells = True ' true means yes to merge the cells
    .Range("B2:H2").HorizontalAlignment = xlCenter
    .Tab.Color = RGB(0, 0, 0) ' tab colour for the Table of Contents sheet is black background colou
    .Range("B2:H2").BorderAround _
    LineStyle = xlContinuous_
    Weight = xlMedium_
    ColorIndex = xlAutomatic

    .Range("B3") = "To create a new TOC press Ctrl + Shift + T"
    .Range("B3").Font.Italic = True
    .Range("B3:H3").MergeCells = True ' true means yes to merge the cells
    .Range("B3:H3").HorizontalAlignment = xlCenter

    .Range("B4") = "Hidden sheets are shown in red text"
    .Range("B4").Font.Italic = True
    .Range("B4:H4").MergeCells = True ' true means yes to merge the cells
    .Range("B4:H4").HorizontalAlignment = xlCenter

    End With
    'To set the number of rows between each hyperlink on the Table of Contents sheet
    Set rng = sht.Range("B6") 'i.e. starting row to wrap columns
    For intSheetNum = 2 To wrk.Worksheets.Count
    If rng.Row > 24 Then 'i.e. wrap the column to the next column after the 24 row
    lngRow = rng.Row
    Set rng = rng.Offset(6 - lngRow, 2) ' i.e. start at row number 5 and wrap columns
    lngColumn = rng.Column - 1
    Columns(lngColumn).ColumnWidth = 2
    End If

    Set shtTarget = wrk.Worksheets(intSheetNum)
    strSheetName = shtTarget.Name
    varSubaddress = "'" & strSheetName & "'!A1"

    rng.NumberFormat = "General"
    rng.HorizontalAlignment = xlLeft
    rng.Value = strSheetName
    rng.Hyperlinks.Add Anchor:=rng, Address:="", _
    SubAddress:=varSubaddress, ScreenTip:="Click to go to worksheet"

    'Show hidden worksheets in red font text:
    'The following code created a "Select method worksheet class failed" error message 1004:
    With rng
    If wrk.Sheets(strSheetName).Visible = xlSheetHidden Then
    rng.Hyperlinks.Add Anchor:=rng, Address:="", _
    SubAddress:=varSubaddress, ScreenTip:="Unhide worksheet"
    rng.Font.Color = RGB(255, 0, 0) ' red background colour
    End If
    End With

    Set rng = rng.Offset(intNumRows + 1, 0)
    Columns(rng.Column).EntireColumn.AutoFit

    Next intSheetNum

    sht.Range("B2").Select
    ActiveWindow.DisplayGridlines = False 'to turn off the gridlines on the Table of Contents sheet
    Application.ScreenUpdating = True
    End If

    ' Add a TOC hyperlink to cell A1 in each sheet
    Application.ScreenUpdating = False
    Sheets(2).Activate
    For i = 2 To Sheets.Count
    Sheets(i).Select (False)

    Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Cells(1), Address:="", SubAddress:= _
    "'Table of Contents'!A1", TextToDisplay:="TOC", ScreenTip:="Click to go to Table of Contents"
    Next

    'Exit the procedure:
    Exit_Proc:
    Set rng = Nothing
    Set sht = Nothing
    Set shtTarget = Nothing
    Set wrk = Nothing
    Exit Sub

    Err_Handler:
    MsgBox Err & ": " & Err.Description
    Resume Exit_Proc
    End Sub
    Last edited by FrankWade; 01-26-2015 at 02:11 AM. Reason: introduction

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166
    Hello FrankWade,

    Welcome to Excelforum. Be a part of large Excel community. Enjoy Learning.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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] Run Time Error '1004': Select method of Range Class failed VBA
    By jcgonzales in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2014, 01:39 AM
  2. Run time error 1004 select method of worksheet class failed
    By ayvee0 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2013, 10:12 AM
  3. [SOLVED] error 1004 select method of range class failed
    By Lee_wwfc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-23-2013, 12:34 PM
  4. Run time error 1004 select method of class failed
    By Doruli in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-14-2013, 04:50 AM
  5. Error 1004: Select method of OptionButton class failed
    By jrubin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2005, 08:51 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