Object Required in Excel VBA
Object required is an error which is caused at run time when we have defined any variable which is not an object but we try to assign some values using a SET statement. This error is a run time error that arises for various reasons. Though this error has its own causes there are also solutions for this error. Every method requires an object qualifier and these objects are assigned by using the SET statement. For example, if we have defined any variable which is not an object but we try to assign some values using a SET statement this will cause the error at run time which is object required error. There are also some instances when we do everything right have correct object qualifiers and valid object but we try to assign values to a read-only property then also we will encounter this error.
How to Handle VBA Object Required?
Out of the numerous reasons we saw for the cause of Object Required Error, there are ways in which we can handle this error.
- For the spell mistakes for the variables or the functions in the code, we can use Option Explicit statement to not encounter this error.
- We can check whether the object we are referring to it exists or not.
- Also, we need to ensure whether we have defined or declared our variables correctly or not.
Example #1
Let us begin with the first example where this type of error might occur and it is when we misspell a function’s name. For this, follow the below steps:
Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Now we can declare our subprocedure.
Code:
Sub Example1() End Sub
Step 3: Look at the code below what we have in the first example.
Code:
Sub Example1() Application3.WorksheetFunction.Sum (Range("A1:A100")) End Sub
Step 4: The application function has an extra character 3 with it and we run the above code we will encounter the following error.
Example #2
Now let us discuss an example where we will use to set an object where an object is not defined instead. In other words, we will treat a non-object feature as an object. For this, follow the below steps:
Step 1: We will start with another subprocedure.
Code:
Sub Example2() End Sub
Step 2: Let us declare a variable for the path or a location to save as a string data type.
Code:
Sub Example2() Dim your_path As String End Sub
Step 3: Let us use the Set statement to set a path to this variable.
Code:
Sub Example2() Dim your_path As String Set your_path = "x/y/z" End Sub
Step 4: For this example’s sake let us use Msgbox function to see what the final result will be.
Code:
Sub Example2() Dim your_path As String Set your_path = "x/y/z" MsgBox your_path End Sub
Step 5: When we execute the above code we will get the following result.
We received this error because we used SET statement to a string variable and VBA treated this variable as an object with the SET statement.
Example #3
Sometimes we encounter this error when we don’t use SET statement when we assign an object reference. Let us go through this example and see how it may occur. For this, follow the below steps:
Step 1: In the same module let us start with the procedure for example 3.
Code:
Sub Example3() End Sub
Step 2: Declare any variable as a variant.
Code:
Sub Example3() Dim ABC End Sub
Step 3: Let us create an object using the Create Object statement.
Code:
Sub Example3() Dim ABC ABC = CreateObject("Excel.Application") End Sub
Step 4: Now we have assigned the object reference but instead of using the SET statement.
Code:
Sub Example3() Dim ABC ABC = CreateObject("Excel.Application") ABC.Visible = True End Sub
Step 5: Once we execute the code above.
Example #4
Now there another chance when we encounter this error and that is when we try to assign values to a read-only property. Our object reference may be correct in this case but we will still encounter an error. Let us go through another example of how this might happen. For this, follow the below steps:
Step 1: In the same module let us begin.
Code:
Sub Example4() End Sub
Step 2: Below is the sample code for using a string variable with an undefined variable.
Code:
Sub Example4() Dim a As String a = "Anand" For i = 1 To l With Age Set a = Age End With Next i End Sub
Step 3: When we execute the code above we will see the following error.
We received this error because we tried to assign values to read-only properties. Let me explain the code first we started a loop from where we are assigning object references but we are using the read-only properties.
Explanation of VBA Object Required:
From the above examples, it is very clear to us that Object Required is a run time error in VBA which we encounter while making some very small mistakes to some huge mistakes in VBA. Error handling this error can be tricky, as some mistakes are hard to identify. But there are some preventive methods such as using the option explicit statement or using the SET statement to assign objects only.
Things to Remember
There are few things which we need to remember about VBA Object Required and they are as follows:
- Object Required is a type of run time error in VBA.
- This error has an error code as 424.
- Spelling mistakes for variables and functions can also be a cause of Object Required Error.
- When some variable is not defined as an object but it is used as an object we may encounter Object Required error.
Recommended Articles
This is a guide to the VBA Object Required. Here we discuss how to handle Object Required in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –