+ Reply to Thread
Results 1 to 6 of 6

Error 424 when including declaration

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2012
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    3

    Error 424 when including declaration

    Hi,
    I have a weird problem with the following code. When the declaration of s is included, the program stops at the "For Each s" statement caliming to need an object. When I comment it out, the code runs error free and TypeName reports "s" to be of type "Worksheet".

    Why does the code fail when the declaration of s is included?


    Sub Patch_Label_Sheets(S1 As Worksheet, S2 As Worksheet, S3 As Worksheet, S4 As Worksheet)
    '**************************************************************************
    ' Changes references so they point correctly to the new sheets
    ' Version:
    '**************************************************************************
        
        Dim Sheets As Variant
        Dim names As Variant
        Dim xx As String
        Dim ss As Long
        Dim s As Worksheet		' When this declaration is commented out, and Option Explicit is not in,
    				' the code runs error free!
        Dim i As Long  		
     
           
        Sheets = Array(S1, S2, S3, S4)
        xx = S1.Name
        
        names = Array("Unshipped items", "Shipping table", "Label report", "Confirmation report")
        ss = LBound(Sheets)
        ss = UBound(Sheets)
        MsgBox TypeName(Sheets)
        
        For Each s In Sheets   	' When the above decalration is included, this statement fails,
    				' with error 424 requiring an object
    
        MsgBox TypeName(s)	 	' when the code runs and s is reported to be of type 'Worksheet'
    
            For i = 0 To UBound(names)
                s.Cells.Replace What:="'x_" & names(i) & "'", Replacement:="'" & Sheets(i).Name & "'", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False
            Next
        Next
    End Sub
    Last edited by bk1; 03-25-2012 at 02:28 PM. Reason: forgot to ask for notification

  2. #2
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Error 424 when including declaration

    I'm not sure if this is the reason but you definitely should rename your Sheets variable as this is the name of an existing Excel VBA collection so you may well get conflicts. Call it something else eg mySheets

  3. #3
    Registered User
    Join Date
    03-25-2012
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Error 424 when including declaration

    Unfortunately, that did not help. However, I do agree with your general viewpoint :-)

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,995

    Re: Error 424 when including declaration

    Dim s as Variant
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    03-25-2012
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Error 424 when including declaration

    Much obliged :-) Thanks!!! That did the trick.

    But why does it have to be a variant when each element of the array is a Worksheet object?Is this documented somewhere?

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,995

    Re: Error 424 when including declaration

    It's documented in Using Arrays in Help. You are iterating through a array, therefore, the control variable (element) must be of type Variant.
    This works, yours did not:
    Sub test()
        Dim Sht As Worksheet
        For Each Sht In ThisWorkbook.Sheets
            Debug.Print Sht.Name
        Next Sht
    End Sub
    so does:
    Sub test()
    Dim TestCell As Range
    
    For Each TestCell In Range("A1:A10")
        Debug.Print TestCell.Address
    Next TestCell
    End Sub

+ 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