excel vba error 424 object required when calling sub

'Excel VBA Error 424: Object required when calling sub.

'Error 424 is raised when an object is required but not provided.

'For the following Sub an argument of type Range is required. A Range is 
'an object in Excel VBA. So when calling this Sub, the calling code must
'pass a Range reference to the Sub:
  
Sub MySub(r As Range)

End Sub

'It is important to realize that in VBA an array is NOT an object and an
'array is certainly NOT a Range. If the calling code attempts to pass an 
'array to the above procedure, VBA will raise the 424 error. For example:

Dim v
v = Range("A1:A10").Value
MySub v		'<--This is an error (424) because 'v' points to an array.


'Also, be careful with parentheses:
  
Dim r as Range
r = Range("A1:A10")
MySub r		'<--This works
MySub (r)	'<--This doesn't work and raises Error 424.

'Placing parenthese around a variable argument when calling a Sub forces VBA
'to evaluate that variable BEFORE the call to the Sub takes place. When an
'object variable is evaluated, VBA creates a temporary variant array of
'all of the values in the object... and that variant array is then passed 
'to the Sub instead of the object... and since the Sub itself
'is expecting an object (in this case a Range object) VBA throws the error 424
'when the array is passed instead of the Range object. This mistake is common.
'
'
'Note: There is one caveat here, the 'Call' keyword:

Call MySub(r)	'<--This works
Call MySub((r))	'<--This doesn't work and raises Error 424.

'When using the 'Call' keyword to execute a Subroutine, parentheses must
'be used for the Sub's argument list (if it has arguments). But notice
'that there is NO SPACE between the name of the Sub and its argument
'list.
'
'
'


  

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source