Error when calling a sub

hazmat

New Member
Joined
Jun 14, 2019
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
I have the following code , inserted within the Worksheet_Change event, and it works fine there.
However, if i make this exact code a stand alone sub routine, Private Sub Test(), and Call Test in the Worksheet_Change Sub, it gives me 'Run-time error 424: Object required, when the first IF Not Intersect.....line runs.
I don't understand why? Thanks
VBA Code:
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    If Not Intersect(Target, Range("AF8:AF157")) Is Nothing Then
        Range("E8:E37").Value = Range("AF8:AF37").Value
        Range("I8:I37").Value = Range("AF38:AF67").Value
        Range("L8:L37").Value = Range("AF68:AF97").Value
        Range("O8:O37").Value = Range("AF98:AF127").Value
        Range("R8:R37").Value = Range("AF128:AF157").Value
    End If

    If Not Intersect(Target, Range("E8:E37,I8:I37,L8:L37,O8:O37,R8:R37")) Is Nothing Then
        Range("AF8:AF37").Value = Range("E8:E37").Value
        Range("AF38:AF67").Value = Range("I8:I37").Value
        Range("AF68:AF97").Value = Range("L8:L37").Value
        Range("AF98:AF127").Value = Range("O8:O37").Value
        Range("AF128:AF157").Value = Range("R8:R37").Value
    End If

    Application.EnableEvents = True
    Application.ScreenUpdating = True
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
When using the variable "Target" in a Worksheet_Change event, the variable "target" is automatically recognized as the cell being changed without having to define it. When you use "Target" in a regular module, it is undefined so it produces an error. I would suggest using the Worksheet_Change event rather than calling the macro from the Worksheet_Change event.
 
Upvote 0
However, if i make this exact code a stand alone sub routine, Private Sub Test(), and Call Test in the Worksheet_Change Sub, it gives me 'Run-time error 424: Object required

If you have placed your code in a standard module then you will need to add a parameter so you can pass Target as an argument to it.

Something like this

Your code in standard module

VBA Code:
Sub Test(ByVal Target As Range)

    On Error GoTo myerror
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    If Not Intersect(Target, Range("AF8:AF157")) Is Nothing Then
        Range("E8:E37").Value = Range("AF8:AF37").Value
        Range("I8:I37").Value = Range("AF38:AF67").Value
        Range("L8:L37").Value = Range("AF68:AF97").Value
        Range("O8:O37").Value = Range("AF98:AF127").Value
        Range("R8:R37").Value = Range("AF128:AF157").Value
    End If

    If Not Intersect(Target, Range("E8:E37,I8:I37,L8:L37,O8:O37,R8:R37")) Is Nothing Then
        Range("AF8:AF37").Value = Range("E8:E37").Value
        Range("AF38:AF67").Value = Range("I8:I37").Value
        Range("AF68:AF97").Value = Range("L8:L37").Value
        Range("AF98:AF127").Value = Range("O8:O37").Value
        Range("AF128:AF157").Value = Range("R8:R37").Value
    End If

myerror:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

and to call it from the change event

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Call Test(Target)
End Sub

Dave
 
Upvote 0
Solution
Worksheet module
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AF8:AF157")) Is Nothing Or Not Intersect(Target, Range("E8:E37,I8:I37,L8:L37,O8:O37,R8:R37")) Is Nothing Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Set Rng = Target
    Call Macro
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End If

    End Sub
Standard module
VBA Code:
Option Explicit
Public Rng As Range

Sub Macro()

     If Not Intersect(Rng, Range("AF8:AF157")) Is Nothing Then

       Range("E8:E37").Value = Range("AF8:AF37").Value
        Range("I8:I37").Value = Range("AF38:AF67").Value
        Range("L8:L37").Value = Range("AF68:AF97").Value
        Range("O8:O37").Value = Range("AF98:AF127").Value
        Range("R8:R37").Value = Range("AF128:AF157").Value
    End If

    If Not Intersect(Rng, Range("E8:E37,I8:I37,L8:L37,O8:O37,R8:R37")) Is Nothing Then
        Range("AF8:AF37").Value = Range("E8:E37").Value
        Range("AF38:AF67").Value = Range("I8:I37").Value
        Range("AF68:AF97").Value = Range("L8:L37").Value
        Range("AF98:AF127").Value = Range("O8:O37").Value
        Range("AF128:AF157").Value = Range("R8:R37").Value
    End If
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top