View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Best practice for executing code

Unless you're coding in NET, you're missing some Set functions.

t = Target
should be
Set t = Target

r = Range("A:C")
should be
Set r = Range("A:C")

i = Intersect(t,r)
should be
Set i = Intersect(t,r)

Next, you need to get the column number of i in your Select statement.

Select Case i
should be
Select Case i.Column

Beyond that, your code looks reasonably good. You'll want to disable events
before your code makes any changes to the worksheet.

Application.EnableEvents = False
' code
Application.EnableEvents = True

--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



"Steven Sutton" wrote in message
...
I am rather new to programming in Excel so I am hoping someone will help me
out here. I want to execute certain subroutines depending on which cell
the
user has just entered data into. Basically I want to check the user's
input
for validity. From what little I know of Excel programming I am thinking
of
doing something like this in the Worksheet_Change event:

Private Sub Worksheet_Change(ByVal Target as Excel.Range)

Dim t as Range
Dim r as Range
Dim i as integer

t = Target
r = Range("A:C") 'where columns A through C are the ones I want to check

i = Intersect(t,r)

Select Case i
Case 1
'insert code for if user has left Column A
Case 2
'Code for if user has left Column B
Case 3
'Code for if user has left Column C
Case Else
exit sub
End Select


Does this make sense? Is there a better way to execute code when a user
leaves a cell?

As always, I thank everyone for there time and assistance.