PDA

View Full Version : Automatic change content of a cell in table using dropdown list



jaimes7
10-14-2019, 04:37 AM
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

gmaxey
10-14-2019, 05:51 AM
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.

25277

jaimes7
10-14-2019, 06:54 AM
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/Tmp2QWh




https://ibb.co/DLktpSn
https://ibb.co/DLktpSn


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


Sorry for the inconvenience

gmaxey
10-14-2019, 07:10 AM
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

jaimes7
10-15-2019, 11:11 PM
Okay sorry man.
Please, help me at least in order to solve that problem.

gmaxey
10-16-2019, 04:44 AM
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?

jaimes7
10-17-2019, 05:29 AM
I already solved the problem removing the vertically merged cells from the table :thumb:friends:

Okay sorry man..
For the next time I will comply with this.


Thank you
Regards