Consulting

Results 1 to 5 of 5

Thread: Getting dropdown to repeat in new table row

  1. #1

    Getting dropdown to repeat in new table row

    I have put the following code in a Dropdown list in the “Change” event. It works great but when the “Add Row” button is clicked the items in the dropdown of the new row do not appear. How do I get the code to repeat for each row? Any help is greatly appreciated!


    HTML Code:
    var newVal = this.boundItem(xfa.event.newText);  // Stores the dropdown value when the user selects something from the drop down list.
    switch (newVal)   // Depending upon the selections made in the Sector dropdown populates the Table2.Row1.GL_DropDown field
    {
    case "1":
            Table2.Row1.GL_DropDown.clearItems();
            Table2.Row1.GL_DropDown.rawValue =null;
            Table2.Row1.GL_DropDown.addItem("Option 1")
            Table2.Row1.GL_DropDown.addItem("Option 2")
            Table2.Row1.GL_DropDown.addItem("Option 3")
            Table2.Row1.GL_DropDown.addItem("Option 4")
    break;
    case "2":
            Table2.Row1.GL_DropDown.clearItems();
            Table2.Row1.GL_DropDown.rawValue =null;
            Table2.Row1.GL_DropDown.addItem("Module 1")
            Table2.Row1.GL_DropDown.addItem("Module 2")
           Table2.Row1.GL_DropDown.addItem("Module 3")
           Table2.Row1.GL_DropDown.addItem("Module 4")    
    break;
    
    case "3":
            Table2.Row1.GL_DropDown.clearItems();
            Table2.Row1.GL_DropDown.rawValue =null;
            Table2.Row1.GL_DropDown.addItem("Station 1")
            Table2.Row1.GL_DropDown.addItem("Station 2")
            Table2.Row1.GL_DropDown.addItem("Station 3")
            Table2.Row1.GL_DropDown.addItem("Station 4")
    break;
    default:
    break;
    }
    Last edited by Aussiebear; 02-13-2025 at 01:06 AM.

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    413
    Location

    Getting dropdown to repeat in a new table row

    This is code behind a Word document?

    Could you attach that file?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    So sorry I posted this in the wrong forum!

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    Maybe something like this (using vba)

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim tbl As ListObject
        Dim newRow As Range
        Dim dropdownColumn As Range 
        ' The column where you want the dropdown
        ' Set the table object (replace "MyTable" with your table's name)
        Set tbl = ThisWorkbook.Sheets("Sheet1").ListObjects("MyTable") 
        ' Adjust sheet name abd table name to suit
        ' Set the column where you want the dropdown (e.g., column B).  Use column number.
        Set dropdownColumn = tbl.DataBodyRange.Columns(2) 
        ' ADJUST COLUMN NUMBER (2 = Column B, 3 = C, etc.) 
        ' Check if the change occurred within the table's data body
        If Not Intersect(Target, tbl.DataBodyRange) Is Nothing Then
            ' Check if a new row was added (more rows in the table now)
            If tbl.DataBodyRange.Rows.Count > Target.Rows.Count Then
                ' Find the newly added row(s)
                Set newRow = Intersect(Target, tbl.DataBodyRange)
                ' Loop through the newly added rows and add data validation
                Dim cell As Range
                For Each cell In newRow.Cells
                    ' Only apply to the dropdown column 
                    If Not Intersect(cell, dropdownColumn) Is Nothing Then
                        With cell.Validation
                            .Delete 
                            ' Clear any existing validation
                            .Add xlValidateList, xlValidAlertStop, xlBetween, "=MyDropdownList" 
                            ' Use your named range. 
                            ' Adjust named range if needed
                            .ErrorMessage = "Invalid Input"
                            .ErrorTitle = "Selection Error"
                        End With
                    End If
                Next cell
            End If
        End If
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,796
    Location
    That looks like code for an Adobe form to me?
    Be as you wish to seem

Posting Permissions

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