Code giving error when in module

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
So, I'm having a problem with some code in VBA, I bet that it's an easy solution, but I'm a noob.
I created a module, to check if it should call another module to sort my table.
This code is throwing me an error "Object required" and it's pointing to the line "If Not Intersect(col, Target) Is Nothing Then".

I don't know how to fix this and already tried a lot of ideas.
This code was working when I had it in the worksheet, but as soon as I created the module, I'm having errors. Already fixed a few, but can't find a solution to this one.


This is in the first worksheet:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Call InstructionsToSort
End Sub


This is in a module (chbx is an ActiveX checkbox):
VBA Code:
Sub InstructionsToSort()
    Dim col As Range
    Dim chbx As Variant
    
    
    Set col = ActiveSheet.ListObjects("Abastecimentos").ListColumns("Data").Range
    Set chbx = ActiveSheet.AutoSort
    
    If Not Intersect(col, Target) Is Nothing Then
        If chbx.Value = True Then
            'Selection IS inside the range.
            Call SortTable
        End If
    End If
End Sub


Again, the code that I have in the module was working when I had it directly in the worksheet's code.

Thanks for any help
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You have to pass the Target range object to your InstructionsToSort subroutine as a parameter. Something like this

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Call InstructionsToSort(Target)
End Sub


VBA Code:
Sub InstructionsToSort(ByRef Target As Range)
    Dim col As Range
    Dim chbx As Variant
    
    
    Set col = ActiveSheet.ListObjects("Abastecimentos").ListColumns("Data").Range
    Set chbx = ActiveSheet.AutoSort
    
    If Not Intersect(col, Target) Is Nothing Then
        If chbx.Value = True Then
            'Selection IS inside the range.
            Call SortTable
        End If
    End If
End Sub
 
Upvote 0
Solution
You have to pass the Target range object to your InstructionsToSort subroutine as a parameter. Something like this

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Call InstructionsToSort(Target)
End Sub


VBA Code:
Sub InstructionsToSort(ByRef Target As Range)
    Dim col As Range
    Dim chbx As Variant
   
   
    Set col = ActiveSheet.ListObjects("Abastecimentos").ListColumns("Data").Range
    Set chbx = ActiveSheet.AutoSort
   
    If Not Intersect(col, Target) Is Nothing Then
        If chbx.Value = True Then
            'Selection IS inside the range.
            Call SortTable
        End If
    End If
End Sub
Thanks, that's it.

I tried that, but instead of ByRef, I used ByVal.
Now it makes sense.

Thank you, again
 
Upvote 0
Another thing, you need to define the range where a double-click will call Sub InstructionsToSort, otherwise it will happen on any cell. Say the range is Range("A2:A10").
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Intersect(Target, Range("A2:A10")) Is Nothing Then
            Call InstructionsToSort(Target)
            Cancel = True
    End If

End Sub
 
Upvote 0
Another thing, you need to define the range where a double-click will call Sub InstructionsToSort, otherwise it will happen on any cell. Say the range is Range("A2:A10").
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   
    If Not Intersect(Target, Range("A2:A10")) Is Nothing Then
            Call InstructionsToSort(Target)
            Cancel = True
    End If

End Sub
Yeah, thats a nice idea. So I'm not calling InstructionsToSort everytime.

That way, I could even remove that part of the logic from my module, to avoid having the same logic two times.
But then, it's almost better to put all my code in the worksheet, instead of the module, like I had before...

What's the general consensus? Logic in worksheet or in modules?

Thanks for your help
 
Upvote 0
If the code is specific to the worksheet, keep it there. If it has general applicability beyond a single worksheet, put it in a general code module.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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