VBA Create a different "Target" inside each procedure.

RuiFlora

Board Regular
Joined
Feb 28, 2014
Messages
58
Hello. I am trying to make a code that calls different procedures depending on the ranges I select. The problem is, inside that procedures I have other Intersects to call other procedures. And it gives me errors because the target is not defined on those sub procedures. Here the code I have to better explain my issue:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim Bmw As Range
Dim Audi As Range


Set Bmw= Worksheets("Cars").Range("Bmw")
Set Audi = Worksheets("Cars").Range("Audi")


If Not Intersect(Target, Sheet12.Range("Bmw")) Is Nothing Then
Call BmwProc
ElseIf Not Intersect(Target, Sheet12.Range("Audi")) Is Nothing Then
Call AudiProc
End If
End Sub
_______________________________________________________________________________________
BmwProc()

If Not Intersect(Range("E55SeriesHide"), Target) Is Nothing Then
Call E55SeriesHide
End If
If Not Intersect(Range("E38SeriesHide"), Target) Is Nothing Then
Call E38SeriesHide
End If

End Sub
_______________________________________________________________________________________


AudiProc()

If Not Intersect(Range("A4SeriesHide"), Target) Is Nothing Then
Call A4SeriesHide
End If
If Not Intersect(Range("A6SeriesHide"), Target) Is Nothing Then
Call A6SeriesHide
End If

End Sub
________________________________________________________________________________________

It gaves me the error of "Object required", which is normal because target is not defined in BmwProc().
But if i replace Target by ActiveCell, the code works but only for 1 cell. And I need that being working for the whole selection of cells.
Hope someone can help me in this issue!

Many thanks in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,
you need to pass the range (Target) as an argument to your procedures.

Try this change & see if helps:

Code:
Sub BmwProc(ByVal Target As Range)
    If Not Intersect(Range("E55SeriesHide"), Target) Is Nothing Then
        Call E55SeriesHide
    End If
    If Not Intersect(Range("E38SeriesHide"), Target) Is Nothing Then
        Call E38SeriesHide
    End If
End Sub

And from your worksheet_Change event call like this:

Code:
Call BmwProc(Target)

Hope helpful

Dave
 
Upvote 0
Hi,
you need to pass the range (Target) as an argument to your procedures.

Try this change & see if helps:

Code:
Sub BmwProc(ByVal Target As Range)
    If Not Intersect(Range("E55SeriesHide"), Target) Is Nothing Then
        Call E55SeriesHide
    End If
    If Not Intersect(Range("E38SeriesHide"), Target) Is Nothing Then
        Call E38SeriesHide
    End If
End Sub

And from your worksheet_Change event call like this:

Code:
Call BmwProc(Target)

Hope helpful

Dave

Touché. Perfect!

Many thanks!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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