Consulting

Results 1 to 7 of 7

Thread: Autofill Prompted From Drop Down List Selection

  1. #1
    VBAX Newbie
    Joined
    Apr 2014
    Posts
    3
    Location

    Autofill Prompted From Drop Down List Selection

    Hi guys,

    i am very new at this but am very keen on learning. Please be gentle...
    I was wondering if i can create a form that autofills predefined values into the cells of a table upon Drop down lists selection. I have attached a mock up example of what I mean.


    Level Value* Comment*
    Level 1 10 OK
    Level 2 20 Not bad
    Level 3 30 Better
    Level 4 40 Impressive
    Level 5 50 Perfect

    *i would like these values to be autofilled upon selection of their corresponding Level. i.e. If Level 1 is selected then i would like the Value and Comment field to be autofilled with 10 and OK respectively.

    Thanks in advance for any help,
    Michael

    Word VBA.docx

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    See attached. The document uses dropdown content controls with a content control on-exit macro, which you can access via Alt-F11.
    Attached Files Attached Files
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    See the attachment
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    snb: your code errors-out if the user selects the default text. Plus it outputs an unwanted '_' instead of a space in 'Not bad'. Back to the drawing board ... you have some basic issues to address
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Apr 2014
    Posts
    3
    Location
    Incedible stuff. Thanks so much for your help.

    You have inspired me to skill up get a grip on the whole VBA thing. So far I have just finished watching some MSDN webinars and about to read some recomended material. It looks like a very step but rewarding learning curve.

    Michael

  6. #6
    VBAX Newbie
    Joined
    Apr 2014
    Posts
    3
    Location
    Paul,

    I kind of get it i think. Here is my newbie take on your code.

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 'I have not come across this sub naming before

    Dim i As Long 'Creating a vaiable i
    With ContentControl.Range 'Defining the drop down list as a group
    If .Information(wdWithInTable) = False Then Exit Sub 'Not to sure why an if statement is here.
    i = .Cells(1).RowIndex 'i is the first row in the selection. Why is "i" defined here and not under Dim above?
    Select Case Split(.Text, " ")(1) 'Here is how you linked i to the drop down list range. Not exactly sure on this one.

    Case 1
    With .Tables(1).Rows(i) 'This identifies the selection made and enters the specified text.
    .Cells(2).Range.Text = 10
    .Cells(3).Range.Text = "OK"
    End With

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    More like:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 'A built-in Document event macro.

    Dim i As Long ' Declare a numeric variable.

    With ContentControl.Range ' Define the range we're working with.

    If .Information(wdWithInTable) = False Then Exit Sub ' We don't want to do anything with content controls that aren't in a table.

    i = .Cells(1).RowIndex ' Get what table row we're on and populate the variable with it.

    Split(.Text, " ")(1) ' Use the Split function to get the Level # from the Content Control.

    Select Case Split(.Text, " ")(1)
    Case # ' Use Select Case structure to process the Level #s.
    End Select

    With .Tables(1).Rows(i) ' Point to our table row.
    .Cells(2).Range.Text = ## ' Insert a number into the second cell in our row.
    .Cells(3).Range.Text = "??" ' Insert text into the third cell in our row.
    End With
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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