vbaclassmismatchbyref

VBA ByRef Error Passing a Class Object to a Sub


I'm trying to pass an object to a new sub but keep hitting a ByRef Mismatch error.

I've declared my object as:

Dim targetWorkbook
Set targetWorkbook = New CWorkbooks

I'm calling my sub by using:

checkbook targetWorkbook

And my sub is set as:

Sub checkbook(targetWorkbook As CWorkbooks)

'Checking if passthrough is working

End Sub

Any help is appreciated, my types are aligned and everything so I'm not sure why this is occuring.

Thanks!


Solution

  • I was able to duplicate your problem with the compiler. The following passes the compiler and runs. You declared TargetWorkbook as Variant, then set it to CWorkbooks - this works, but not when passed to the sub.

    Sub main()
        Dim TargetWorkbook As CWorkbooks
        Set TargetWorkbook = New CWorkbooks
        checkbook TargetWorkbook
    End Sub
    Sub checkbook(ByRef TargetWorkbook As CWorkbooks)
    
    'Checking if passthrough is working
    
    End Sub