Consulting

Results 1 to 7 of 7

Thread: Automatic change content of a cell in table using dropdown list

  1. #1

    Automatic change content of a cell in table using dropdown list

    Hello everyone,

    I need some help in order to solve a problem related with MS Word VBA.

    The problem is the following:

    I have a car table that has 3 columns ("Model", "Year", "Price").
    In each cell of the "Model" column I have a dropdown list. I want that: every time I change the car model in the dropdown list, it updates the price that is in the same row, although in the third column ("Price"). How do I do that?

    I ask for help please, asap

    Thanks in advance
    Regards

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    If you put a dropdownlist content control titled "Model" in the first column. Use the model name as the list entry text and the price as the list entry value then you could use the following in the ThisDocument module of the project:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim lngIndex As Long
      Select Case ContentControl.Title
        Case "Model"
          For lngIndex = 1 To ContentControl.DropdownListEntries.Count
            If ContentControl.Range.Text = ContentControl.DropdownListEntries(lngIndex).Text Then
              ContentControl.Range.Rows(1).Cells(3).Range.Text = ContentControl.DropdownListEntries.Item(lngIndex).Value
              Exit For
            End If
          Next lngIndex
      End Select
    lbl_exit:
      Exit Sub
    End Sub
    Note: This method requires you to physically exit the the content control after making your choice.

    There are other ways but much more complicated. Contact me if interested.

    Choose an item.docm
    Last edited by gmaxey; 10-14-2019 at 06:13 AM.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    I already used the model name as the list entry text, and the price as the list entry value.


    But I am getting an error after put your code, as you can see here:



    https://ibb.co/Tmp2QWh





    https://ibb.co/DLktpSn


    Please help me, coz I don't have skills in vba macro programming.


    Sorry for the inconvenience

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Your error message told you what the problem is. Your description simply said you have a three column table. Your picture is not a three column table. It is a complex table with vertically merged rows. Do people pay you to sell cars? You should hire someone and pay them to write your code.

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim lngIndex As Long
      Select Case ContentControl.Title
        Case "Model"
          For lngIndex = 1 To ContentControl.DropdownListEntries.Count
            If ContentControl.Range.Text = ContentControl.DropdownListEntries(lngIndex).Text Then
              ContentControl.Range.Cells(1).Next.Next.Next.Next.Next.Next.Range.Text = ContentControl.DropdownListEntries.Item(lngIndex).Value
              'ContentControl.Range.Rows(1).Cells(3).Range.Text = ContentControl.DropdownListEntries.Item(lngIndex).Value
              Exit For
            End If
          Next lngIndex
      End Select
    lbl_exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Okay sorry man.
    Please, help me at least in order to solve that problem.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    I already, helped you solve that problem. You either use a table that doesn't have vertically merged cells or you use some variation of the code I posted last.

    You didn't answer my question. Do you give transportation solutions (cars) away or do you sell them? Why do you expect people here to give you VBA solutions on a plate?
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    I already solved the problem removing the vertically merged cells from the table

    Okay sorry man..

    For the next time I will comply with this.


    Thank you
    Regards

Tags for this Thread

Posting Permissions

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