Consulting

Results 1 to 4 of 4

Thread: Solved: If Then code

  1. #1

    Solved: If Then code

    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!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]

    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thank you very much!! It works great now!!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For more complex situations, see Contextures.Com
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •