Code giving run-time error when called, but not when placed on worksheet.

FrancisM

Board Regular
Joined
Apr 12, 2016
Messages
139
The code listed below runs on the worksheet, but gives a Run-time error '424': Object required when called. I called the code, because when placed on the worksheet, it prevented other code from running. I was thinking that if the code was called it would allow the other code to run.

VBA Code:
Sub Do_Carryover()

'Reminds the user to enter the carryover name into the current FY listing.
Dim MsgBoxResult As Long
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B4:B17")) Is Nothing Then Exit Sub
       MsgBoxResult = MsgBox("Is the Veteran a a new client to Career Link? " & vbCr, _
       vbYesNo, "Vocational Services - OVR " & ActiveSheet.Name)
       If MsgBoxResult = vbNo Then
     
       MsgBox " Check to verify Veteran data is entered in FY ##  Referrals! " & vbCr & _
                   "" & vbCr & _
                   " It's critical that Veteran data is captured. " & vbCr & _
                   "" & vbCr & _
                   " Please enter the name in walk in list if not on this year's consult list! " & vbCr & _
                   "Do not enter the name on the Walk In list of there is a Consult for this FY! " & vbCr & _
                   "" & vbCr & _
                   " Enter veteran as a walk in, if a carry over from the past FY year, and enter the SC percent! " & vbCr & _
                   "" & vbCr & _
                   " You have entered " & Cells(Target.Row, 2) & " in cell " & Target.Address, vbInformation, "Vocational Services - Career Link" & ActiveSheet.Name
           
                Call Referals 'Calls Referrals folder.
               
       ElseIf MsgBoxResult = vbYes Then
            If WorksheetFunction.CountA(Range("B:B")) <> 0 Then
            MsgBox " Check to verify Veteran data is entered in FY ##  Referrals! " & vbCr & _
                   "" & vbCr & _
                   " It's critical that Veteran data is captured. " & vbCr & _
                   "" & vbCr & _
                   " Please enter the name in walk in list if not on this year's consult list! " & vbCr & _
                   "Do not enter the name on the Walk In list of there is a Consult for this FY! " & vbCr & _
                   "" & vbCr & _
                   " Enter veteran as a walk in, if a carry over from the past FY year, and enter the SC percent! " & vbCr & _
                   "" & vbCr & _
                   " You have entered " & Cells(Target.Row, 2) & " in cell " & Target.Address, vbInformation, "Vocational Services - Career Link" & ActiveSheet.Name
           
                Call Referals 'Calls Referrals folder.
            Else
                Exit Sub
                End If
                End If
End Sub

Please tell me what I am doing wrong. Will calling the code allow the other code to run?
 
Oh ok I apologise. The reason you cant use target in a normal macro as you do in a worksheet change macro is that target is set in the worksheet change macro. If you want to use target like that in a normal macro you need to set it first to tell the macro what Target is. Eg 'Set Target = Selection'
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Oh ok I apologise. The reason you cant use target in a normal macro as you do in a worksheet change macro is that target is set in the worksheet change macro. If you want to use target like that in a normal macro you need to set it first to tell the macro what Target is. Eg 'Set Target = Selection'
I apologize. I should have made my question statement clearer. So you are saying that it would be something like Set target = ("B4:B17")
 
Upvote 0
If you want to keep them separate, then use
VBA Code:
Call Do_Carryover(Target)
and change the sub header to
Code:
Sub Do_Carryover(Target as Range)
 
Upvote 0
If you are calling a procedure that is in a standard module from you worksheets code page and that procedure needs to work with Target range then as @steve the fish identified, you need tell your code what target is - you can do this by passing it as an argument to the procedure.

Add the parameter Target to your procedure

VBA Code:
Sub Do_Carryover(ByVal Target As Range)

'rest of code



And then pass it when calling from your worksheet

Code:
Call Do_Carryover(Target)


If the range your code is working with a sheet that is not in the activesheet then you should qualify the ranges in the called code to Target.Parent.Range

Hope Helpful

Dave
 
Upvote 0
I added the lines that you suggested. It appears to not run, But also not give an error message. Is it possible to include a small sample? If so How?
 
Upvote 0
Are you saying the Carryover sub doesn't run?
 
Upvote 0
I added the lines that you suggested. It appears to not run, But also not give an error message. Is it possible to include a small sample? If so How?

If the sheet your Do_Carryover code is meant to be referring to is not the active sheet then this could be a factor

qualify the range as shown below & see if resolves

Rich (BB code):
If Intersect(Target, Target.Parent.Range("B4:B17")) Is Nothing Then Exit Sub

Dave
 
Upvote 0
Add the word stop as shown
Rich (BB code):
Sub Do_Carryover()
Stop
'Reminds the user to enter the carryover name into the current FY listing.
Dim MsgBoxResult As Long
Then change something on the sheet, what happens?
 
Upvote 0
@Fluff & @dmt32 . It was a combination of both your suggestions & a little on my part (VERY little).
Here is the worksheet code: Call Do_Carryover(Target)
Here is the module code:
VBA Code:
Option Explicit
Sub Do_Carryover(ByVal Target As Range)
'Reminds the user to enter the carryover name into the current FY listing. ' prevents (prevents modification or deletion of cells code from running)
Dim MsgBoxResult As Long
If Intersect(Target, Target.Parent.Range("B4:B17")) Is Nothing Then Exit Sub
       MsgBoxResult = MsgBox("Is the Veteran a a new client to Career Link? " & vbCr, _
       vbYesNo, "Vocational Services - OVR " & ActiveSheet.Name)
       If MsgBoxResult = vbNo Then
      
      MsgBox " Check to verify Veteran data is entered in FY ##  Referrals! " & vbCr & _
                   "" & vbCr & _
                   " It's critical that Veteran data is captured. " & vbCr & _
                   "" & vbCr & _
                   " Please enter the name in walk in list if not on this year's consult list! " & vbCr & _
                   "Do not enter the name on the Walk In list of there is a Consult for this FY! " & vbCr & _
                   "" & vbCr & _
                   " Enter veteran as a walk in, if a carry over from the past FY year, and enter the SC percent! " & vbCr & _
                   "" & vbCr & _
                   " You have entered " & Cells(Target.Row, 2) & " in cell " & Target.Address, vbInformation, "Vocational Services - Career Link" & ActiveSheet.Name
            
                Call Referals 'Calls Referrals folder.
                
       ElseIf MsgBoxResult = vbYes Then
            If WorksheetFunction.CountA(Range("B:B")) <> 0 Then
               MsgBox " Check to verify Veteran data is entered in FY ##  Referrals! " & vbCr & _
                   "" & vbCr & _
                   " It's critical that Veteran data is captured. " & vbCr & _
                   "" & vbCr & _
                   " Please enter the name in walk in list if not on this year's consult list! " & vbCr & _
                   "Do not enter the name on the Walk In list of there is a Consult for this FY! " & vbCr & _
                   "" & vbCr & _
                   " Enter veteran as a walk in, if a carry over from the past FY year, and enter the SC percent! " & vbCr & _
                   "" & vbCr & _
                   " You have entered " & Cells(Target.Row, 2) & " in cell " & Target.Address, vbInformation, "Vocational Services - Career Link" & ActiveSheet.Name
            
                Call Referals 'Calls Referrals folder.
            Else
                Exit Sub
                End If
                End If
 
    End Sub
Now I will test the other problem lines & see if they are still a problem. I also want to tank @steve the fish for his help, he explained that I had to tell the macro what the target is. Thank you all.
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,789
Members
449,126
Latest member
Greeshma Ravi

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