PDA

View Full Version : Solved: If Then code



traveler7739
10-23-2010, 05:51 AM
Hello Everyone,

I'm new to VBA code and have a small issue I need help resolving. I have two cells on a user spreadsheet both with data validation drop down lists. I would like the second cell to auto fill if a certain value is selected in the first cell. This is the simple line of code that I wrote to accomplish this:

Sub WidgetInput()
If Range("Input1") = "Item1" Then Range("Input2") = "Widget"
End Sub

How do I change this code so that cell 2 fills with "Widget" automatically when a user selects "Item1" in cell 1?

Thank you very much!!

Bob Phillips
10-23-2010, 06:03 AM
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Me.Range("Input1").Address Then

If Target.Value = "Item1" Then Me.Range("Input2") = "Widget"
End If
End Sub


This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.

traveler7739
10-23-2010, 06:10 AM
Thank you very much!! It works great now!!

mdmackillop
10-23-2010, 06:22 AM
For more complex situations, see Contextures.Com (http://www.contextures.com/xlDataVal02.html)