Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter Event in Excel VBA.

Status
Not open for further replies.

HelloLloyd

Technical User
Dec 27, 2002
18
US
I am trying to write a VBA procedure that is called when the active cell in excel is within a certain range of cells. For example, the cell range of interest is C25:C50. I want the VBA procedure to be called with the active cell is one of the cells in C25:C50. I don't know if this is even possible. Any help is greatly appreciated.
 
give this a try

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Column = 3 Then
If Target.Row >= 25 Or Target.Row <= 50 Then
MsgBox &quot;your area selected&quot;, vbOKOnly
End If
End If

End Sub

hope it helps!!
 
This is probably a real basic question, but I have to ask it anyway. I have entered the VBA code and am unable to run the procedure. I see that it needs values passed to it. Do I need to call this procedure from another and pass it the variables it needs? I'm lost. Thanks for the help.
 
this procedure when included in your code always executes when a different cell is selected (ie click tab or enter)(try sticking a breakpoint at &quot;If Target.Column = 3 Then&quot; to see if the event fires!) the variable is &quot;passed&quot; by excel (dont quote my terminology its not very accurate) and contains many properties about that particular cell selected.

are you selecting a single cell or a range of cells?

if its a range of cells a little tinkering may be required!

there was actually a mistake i just spotted,

If Target.Row >= 25 Or Target.Row <= 50 Then

should be

If Target.Row >= 25 AND Target.Row <= 50 Then

hope this gets the ball rolling!
 
I figured out my problem. I wasn't saving the event handler in the right spot. I had it as a general module and it needed to be saved at the sheet level to work. It works great now though. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top