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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Excel: Where to place code for Validation List selection. 1

Status
Not open for further replies.

Wozza

Programmer
Jan 29, 2002
13
GB
Hi Guys,
I have set up a Data Validation to reference a list.
I would like some code to kick in once I have selected a value from the Validation's drop-down list.
I am not too sure where to place the code...

Any thoughts / suggestions will be very appreciated!! ;)

Warren Bennet
Wannabe Programmer.
Check out my other vocation;
 
place it on the worksheet, use the target to reference the cell where your user is selecting the list.



Filmmaker, gentleman and Ambasador for London to the sticks.

 
Warren,

I would like some code to kick in once I have selected a value from the Validation's drop-down list.

There are events that can be trapped by Excel VBA -- worksheet change selection, worksheet change, worksheet activate etc. Right click the sheet tab and select View Code to activate the VBA Editor and display the Code Sheet for the worksheet.

In the Object Box select Workbook

In the Procedure Box select Change

Write your code in...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

End Sub
which will run every time a selection is made from a Data/Validation List.

:)



Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
The Change event is a good place for your code, as Skip suggested. Just be aware that the Change event fires anytime any cell on the sheet is changed. To only run your code when a particular cell/range of cells is changed, you can start your code with something like this:
Code:
Dim c As Range, WatchRange As Range
Set WatchRange = Range("A5:A10") ' can be a single cell as well
Set c = Intersect(Target.Cells(1,1), WatchRange) 
' Target is the cell or group of cells that 
' triggered the Change event. It's set 
' automatically by Excel.
If Not c is Nothing Then ' The top-left cell of
' Target is in WatchRange
   ' put your code in here
End If
Hope you find that helpful!


VBAjedi [swords]
 
Guys, thanks for all your help - it is much appreciated.
'Change' now seems so obvious! :eek:)

VBAjedi - thanks for the extra tip re. 'WatchRange'
I will add this in to my code, as I am experiencing exactly what you have warned - it is mightily frustrating!

Warren Bennet
Wannabe Programmer.
Check out my other vocation;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top