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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
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
 

Emily

Active Member
Joined
Aug 28, 2002
Messages
304
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?
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
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..
 

Forum statistics

Threads
1,148,051
Messages
5,744,520
Members
423,881
Latest member
Nguyen Vu

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
Top