Defining Target Range

Flenley

Board Regular
Joined
Jul 31, 2002
Messages
71
I am trying to teach myself VBA and am having trouble below defining the range. Everything is working except for the fact that any change on the sheet is adding the comment. I only want the code to run if cell a1 = 3 and I don't know where I am going wrong.

Any help would be much appreciated!

Sub worksheet_change(ByVal Target As Range)
If Target.Range("a1") = 3 Then
If MsgBox("How are you", vbOKCancel) = vbOK Then
Range("c4").Select
ActiveCell.AddComment "Outbound Only"
End If
End If

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Flenley,

Target already IS a range. You simply need to check whether it is cell A1 and its value:

Sub worksheet_change(ByVal Target As Range)
If Target.Address = [A1].Address AND Target.Value = 3 Then
If MsgBox("How are you", vbOKCancel) = vbOK Then
Range("c4").Select
ActiveCell.AddComment "Outbound Only"
End If
End If

End Sub
 
Upvote 0
Hi
If target A1 has been changed more than one, error code 1004 appears.
Is it required to add "On Error Resume Next" on top or any alternative suggestion?
 
Upvote 0
On 2002-08-29 21:18, Emily wrote:
Hi
If target A1 has been changed more than one, error code 1004 appears.
Is it required to add "On Error Resume Next" on top or any alternative suggestion?

Hi Emily
Welcom to the Board
Yes you are correct the On Error Resume Next
should be added to this incase Comment has
already being created....If your comment is
changing then you also need to Delete the old
comment and then Add the new comment..
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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