Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2022, 05:49 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default Dropdown list dependency and autofill

Hi

Pretty new to advanced functions in Word, and complete beginner in the world of VBA.

Im trying to create a dropdown list with a secondary, dependent dropdown. First has 2 options: "yes" or "no". In case of "yes", 3 options are presented in the secondary list which will change color according to answer. So far so good!



However, when the "no" option is selected, I would like the secondary dropdown to autofill something along the lines of "N/A" or "—".

None of my attempts at the trusted copy/paste method has worked, so the code below is what I have that works.

Thanks in advance!

Code:
Option Explicit
Dim StrOption As String
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
If CCtrl.Title = "Relevans" Then StrOption = CCtrl.Range.Text
End Sub

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim i As Long, StrOut As String
With CCtrl
  If .Title = "Relevans" Then
    If StrOption = .Range.Text Then Exit Sub
    Select Case .Range.Text
      Case "Ja"
        StrOut = "Uakseptabel,Middels,Akseptabel"
      Case "Nei"
        StrOut = "—"
      Case Else
        .Type = wdContentControlText
        .Range.Text = ""
        .Type = wdContentControlDropdownList
    End Select
    With ActiveDocument.SelectContentControlsByTitle("Risiko")(1)
      .DropdownListEntries.Clear
      For i = 0 To UBound(Split(StrOut, ","))
        .DropdownListEntries.Add Split(StrOut, ",")(i)
      Next
      .Type = wdContentControlText
      .Range.Text = ""
      .Type = wdContentControlDropdownList
    End With
  End If
End With

With CCtrl.Range
    If CCtrl.Title = "Risiko" Then
        Select Case .Text
            Case "Uakseptabel"
                .Cells(1).Shading.BackgroundPatternColor = RGB(255, 0, 0)
            Case "Akseptabel"
                .Cells(1).Shading.BackgroundPatternColor = RGB(0, 176, 80)
            Case "Middels"
                .Cells(1).Shading.BackgroundPatternColor = RGB(255, 255, 0)
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub
Reply With Quote
  #2  
Old 03-14-2022, 07:01 AM
gmayor's Avatar
gmayor gmayor is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I think the following will work for the two controls, however it is not clear what .Cells(1) refers to so I have used cell 1 of the first table

Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim i As Long, StrOut As String
    With CCtrl
        If .Title = "Relevans" Then
            Select Case .Range.Text
                Case "Ja"
                    StrOut = "Uakseptabel,Middels,Akseptabel"
                    If CCtrl.Range.Text = "Ja" Then
                        With ActiveDocument.SelectContentControlsByTitle("Risiko")(1)
                            .Type = wdContentControlDropdownList
                            .DropdownListEntries.Clear
                            .Type = wdContentControlRichText
                            .Range.Text = ""
                            .Type = wdContentControlDropdownList
                            For i = 0 To UBound(Split(StrOut, ","))
                                .DropdownListEntries.Add Split(StrOut, ",")(i)
                            Next i
                        End With
                    End If
                Case "Nei"
                    With ActiveDocument.SelectContentControlsByTitle("Risiko")(1)
                        If .Type = wdContentControlDropdownList Then
                            .DropdownListEntries.Clear
                        End If
                        .Type = wdContentControlRichText
                        .Range.Text = "—"
                    End With
                Case Else
                    With ActiveDocument.SelectContentControlsByTitle("Risiko")(1)
                        If .Type = wdContentControlDropdownList Then
                            .DropdownListEntries.Clear
                        End If
                        .Type = wdContentControlRichText
                        .Range.Text = ""
                    End With
            End Select
        End If
    End With
    If CCtrl.Title = "Risiko" Then
        If CCtrl.ShowingPlaceholderText = False Then
            With CCtrl.Range
                Select Case .Text
                    Case "Uakseptabel": ActiveDocument.Tables(1).Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 0, 0)
                    Case "Akseptabel": ActiveDocument.Tables(1).Range.Cells(1).Shading.BackgroundPatternColor = RGB(0, 176, 80)
                    Case "Middels": ActiveDocument.Tables(1).Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 255, 0)
                    Case Else: ActiveDocument.Tables(1).Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
                End Select
            End With
        End If
    End If
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-14-2022, 07:17 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

That works beautifully! Except the colors in the cells dont change anymore. The cell that is supposed to be colored in, is the one that also holds the text. Does that make sense?

Basically, I need the "risiko" cell to be colored based on the answer. So if you choose "Akseptabel" the cell colors green etc.
Reply With Quote
  #4  
Old 03-14-2022, 07:46 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

After further investigation it isnt working properly unfortunately. Dont know if my explanation is too poor, but I have 35-ish cells which should be independent from eachother.

I'll upload a copy of the table im trying to make work for reference.
Attached Files
File Type: docm Test.docm (35.0 KB, 16 views)
Reply With Quote
  #5  
Old 03-14-2022, 02:55 PM
Guessed's Avatar
Guessed Guessed is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If the applicable Risiko CC is always in the next cell, this should work
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
  Application.ScreenUpdating = False
  Dim i As Long, StrOut() As String, rngRow As Range, ccRis As ContentControl
  With CCtrl
    If .Title = "Relevans" Then
      Set ccRis = .Range.Cells(1).Next.Range.ContentControls(1)
      Select Case .Range.Text
        Case "Ja"
          StrOut = Split("Uakseptabel,Middels,Akseptabel", ",")
          With ccRis
            .Type = wdContentControlText
            .Range.Text = ""
            .Type = wdContentControlDropdownList
            .DropdownListEntries.Clear
            For i = 0 To UBound(StrOut)
              .DropdownListEntries.Add StrOut(i)
            Next i
          End With
        Case "Nei"
          With ccRis
            .Type = wdContentControlText
            .Range.Text = "—"
          End With
        Case Else
          With ccRis
            .Type = wdContentControlText
            .Range.Text = ""
          End With
      End Select
    ElseIf .Title = "Risiko" Then
      Select Case .Range.Text
        Case "Uakseptabel": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 0, 0)
        Case "Akseptabel": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(0, 176, 80)
        Case "Middels": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 255, 0)
        Case Else: .Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
      End Select
    End If
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 03-14-2022, 03:48 PM
macropod's Avatar
macropod macropod is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Create one each of your Relevans & Risiko content controls, both with their relevant drop-down entries, then copy them to each of the other cells. Having done that, run the following macro:
Code:
Sub ConfigureRisiko()
Dim CCtrl As ContentControl
For Each CCtrl In ActiveDocument.ContentControls
  With CCtrl
    If .Title = "Risiko" Then
      .Type = wdContentControlRichText
      .Range.Text = " "
      .LockContents = True
    End If
  End With
Next
End Sub
From then on, the only macro you need in the document is:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim r As Long, StrIn As String
With CCtrl
  r = .Range.Cells(1).RowIndex
  Select Case .Title
    Case "Relevans"
      StrIn = .Range.Text
      With .Range.Tables(1).Cell(r, 5).Range.ContentControls(1)
        If StrIn = "Ja" Then
          If .Type = wdContentControlRichText Then
            .LockContents = False
            .Range.Text = ""
            .Type = wdContentControlDropdownList
            .Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
          End If
        ElseIf StrIn = "Nei" Then
          .LockContents = False
          .Type = wdContentControlRichText
          .Range.Text = "_"
          .Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
          .LockContents = True
        Else
          .LockContents = False
          .Type = wdContentControlRichText
          .Range.Text = " "
          .Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
          .LockContents = True
        End If
      End With
    Case "Risiko"
      If .LockContents = False Then
        Select Case .Range.Text
          Case "Uakseptabel": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 0, 0)
          Case "Akseptabel": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(0, 176, 80)
          Case "Middels": .Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 255, 0)
          Case Else: .Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
      End If
  End Select
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-15-2022, 12:28 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

Both of the last solutions worked! Fantastic, thanks alot for the help.
Reply With Quote
  #8  
Old 03-15-2022, 02:17 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

For some reason this solution removes the dropdown options in the "risiko" cells upon closing and then opening the document. It seems to only be the case if nothing has been filled into the cell before exiting.

Preferably I would like to be able to take a copy of the document before filling anything into the table, so I can make multiple copies of that same doc.

Quote:
Originally Posted by macropod View Post
Create one each of your Relevans & Risiko content controls, both with their relevant drop-down entries, then copy them to each of the other cells. Having done that, run the following macro:
...
From then on, the only macro you need in the document is:
Reply With Quote
  #9  
Old 03-15-2022, 02:14 PM
macropod's Avatar
macropod macropod is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Oops! Add the following code the the document:
Code:
Private Sub Document_Open()
Dim CCtrl As ContentControl, i As Long
With ActiveDocument
  For Each CCtrl In .ContentControls
    With CCtrl
      If .Title = "Risiko" Then
        If .Type = wdContentControlRichText Then
          .LockContents = False
          .Type = wdContentControlDropdownList
          .DropdownListEntries.Add "Uakseptabel"
          .DropdownListEntries.Add "Akseptabel"
          .DropdownListEntries.Add "Middels"
          .Type = wdContentControlRichText
          .LockContents = True
        End If
      End If
    End With
  Next
  .Saved = True
End With
End Sub
or, if being run from a template, add all the code to that template, along with:
Code:
Private Sub Document_New()
Call Document_Open
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 03-17-2022, 01:02 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

This did the trick! Thanks a lot!

Another question: is there anything I can add to control postion and size of the text? I'd like the text t be centered in the cells.
Reply With Quote
  #11  
Old 03-17-2022, 05:34 AM
macropod's Avatar
macropod macropod is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by LustNik View Post
Another question: is there anything I can add to control postion and size of the text? I'd like the text t be centered in the cells.
Simply format the cells as you would if they had ordinary text content.

PS: Please don't quote previous posts in your replies unless there's something specific you need to refer to - and then include only the relevant portion(s). Anything more just adds clutter.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 03-17-2022, 06:22 AM
LustNik LustNik is offline Dropdown list dependency and autofill Windows 10 Dropdown list dependency and autofill Office 2021
Novice
Dropdown list dependency and autofill
 
Join Date: Mar 2022
Posts: 7
LustNik is on a distinguished road
Default

Noted. Thanks for all the help! Much appreciated
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Autofill Dropdown Syed Rashid Word 1 06-22-2021 10:59 AM
Compose Email - Autofill List Style Change RoscoW88 Outlook 2 05-24-2021 06:44 PM
Dropdown list dependency and autofill Autofill a field based on drop down list selection lascough20 Word 2 09-21-2018 02:24 AM
Selection of a dropdown creates another dropdown wih the list krishnamurthy.ka2810 Word VBA 1 04-26-2018 11:44 PM
Dropdown list dependency and autofill Autofill a form which is contingent on a dropdown selection. biffle0764 Word 2 05-09-2012 12:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft