Target.Range

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$C$5" Then
        Call Generate_Newlist
    End If

End Sub

which I want to change to something like

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Range = Range("StartDate") Then
        Call Generate_Newlist
    End If

End Sub

as the value in C5 may change position to say D5 by a user but if I use the range name ("StartDate") then hopefully the code will still work!

When I use the latter code I get an error (argument not optional) on the .Range bit of

Code:
If Target.Range = Range("StartDate") Then
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Great that worked. Thanks.

So when or how is Target.Range used???
 
Upvote 0
I don't think it is. Where have you seen that?

Target is already a range, so I don't know what that would mean. It would be like saying Range("A1").Range.
 
Upvote 0
Great that worked. Thanks.

So when or how is Target.Range used???

When applied to a Range object (which Target is) the Range property expects a reference as its argument, eg Range("A1"). You can't use it on its own like you can with eg the AutoFilter object. Another point is that you can't use = to test the equality of objects.
 
Upvote 0
When you write Target., from the list that appears Range is an option. That is why I thought that Target.Range was valid.
 
Upvote 0
Range("A1") is the first cell in Target. It's not real A1.
Code:
[COLOR="Blue"]If[/COLOR] Target.Range("A1") = Range("StartDate") [COLOR="Blue"]Then[/COLOR]
...
 
Upvote 0
OK I understand. There was obviously a missing piece of the jigsaw for me.
Thanks all.
 
Upvote 0
Thanks Andrew and Sektor - I didn't know that.

Now I just need to find somewhere I can use it...
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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