VBA Target.Address

shamas21

Active Member
Joined
Nov 19, 2007
Messages
280
Hi All

Can someone explain what the Taget.Address relates to in VBA. I cant figure it out, how can this be used? thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here's an example which goes in a sheet's code module (right click the tab > View code)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "A10" Then
    MsgBox "You can't go there!!"
    Range("A1").Select
End If
End Sub
 
Upvote 0
Here's an example which goes in a sheet's code module (right click the tab > View code)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "A10" Then
    MsgBox "You can't go there!!"
    Range("A1").Select
End If
End Sub

Hi VOG

This doesnt seem to work. I have put this into 'Thisworkbook' but i can click anywhere within the sheet and allows me to input data
 
Upvote 0
Hi Shamas!

The address property simply identifies the location of the Target. Note that Target may be more than 1 cell.
You can use it to isolate a cell for macro execution. Example
Code:
If Target.Address  = "$A$1" Then
or
Code:
If Target.Addres <> "$A$1" Then Exit Sub
You can use this property in a messagebox
Code:
MsgBox "Value of " & Target.Address & " is " & Target.Value

Note that Target.Address is ALWAYS an absolute reference and in Caps.

So Target.Address ="A1" or Target.Address = "$a$1" will not work

lenze

Edit: Vog's code should go in the WorkSheet module, not the ThisWorkBook module
 
Last edited:
Upvote 0
It goes in the sheet's code module. Right click the sheet tab and select View Code.
 
Upvote 0
Here's an example which goes in a sheet's code module (right click the tab > View code)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "A10" Then
    MsgBox "You can't go there!!"
    Range("A1").Select
End If
End Sub

VOG what does the False, False signify?
Target.Address (False, False)
 
Upvote 0
Hi Shamas!

The address property simply identifies the location of the Target. Note that Target may be more than 1 cell.
You can use it to isolate a cell for macro execution. Example
Code:
If Target.Address  = "$A$1" Then
or
Code:
If Target.Addres <> "$A$1" Then Exit Sub
You can use this property in a messagebox
Code:
MsgBox "Value of " & Target.Address & " is " & Target.Value

Note that Target.Address is ALWAYS an absolute reference and in Caps.

So Target.Address ="A1" or Target.Address = "$a$1" will not work

lenze

Edit: Vog's code should go in the WorkSheet module, not the ThisWorkBook module


Lenze,

So can the address property be used in another way within a module ?
 
Upvote 0
Target.Address will by default return an absolute address like $A$1 as lenze said.

Target.Address(False, False) makes both the row and column references relative.
 
Upvote 0
shamas

Try highlighting Address in the code and hitting F1.

That should give you context sensitive help for the property.
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,481
Members
452,516
Latest member
archcalx

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