Dynami c выпадающий с другого листа и столбца (смещение / индекс?) - PullRequest
0 голосов
/ 15 апреля 2020

У меня есть документ Excel, содержащий 2 листа, 1 лист импорта и лист данных. Выпадающий список Dynami c в столбце B листа импорта должен зависеть от значения, выбранного в столбце A листа импорта.

Однако, чтобы найти соответствующую «серию», мне нужно сопоставить идентификаторы из техническая спецификация. (Идентификатор Eicher должен совпадать с идентификатором родителя серии; столбцы B и D)

Снимки экрана внизу должны объяснить это лучше;

Я выбрал Eicher в User Sheet.A3, теперь я хочу получить Идентификатор из таблицы данных столбца B (mmcMake-24046283). При этом мне нужно найти все соответствующие серии с одинаковым идентификатором родительской серии. Так что в этом случае мой раскрывающийся список должен был показать; Серия Eicher, серия 2000, серия 3000, серия 300 и серия 400.

User sheet

Data sheet

1 Ответ

1 голос
/ 15 апреля 2020

Хорошо, вот код для вставки проверки. Проверьте часть «установка переменных», чтобы убедиться, что каждая переменная установлена ​​правильно. Извините за довольно сложные имена переменных, но пустой желудок затрудняет синтез. : D

Sub SubDynamicDropdownGenerator()

    'Declarations.
    Dim StrDataSheetName As String
    Dim StrImportSheetName As String
    Dim StrImportColumnMake As String
    Dim StrDataColumns As String
    Dim StrDataColumnSeries As String
    Dim StrDataColumnSeriesParentIDEntire As String
    Dim BytDataColumnMakesIDInternalColumn As Byte
    Dim RngCellWithDropDown As Range

    'Setting variables.
    StrDataSheetName = "Data"                       'Insert here the name of the sheet with data
    StrImportSheetName = "Import"                   'Insert here the name of the sheet with the import (where the range with the dynamic drowpdown is)
    StrImportColumnMake = "A"                       'Insert here the letter of the column where labeled Make (according to your first picture it is A)
    StrDataColumns = "A:E"                          'Insert here the letters of the columns where the data are located in the data sheet (i guess they are A:E)
    StrDataColumnSeries = "C"                       'Insert here the letter of the column where the Series are located in the data sheet (i guess is the C column)
    StrDataColumnSeriesParentIDEntire = "E:E"       'Insert here the address of the column where the Series Parent ID are located in the data sheet (i guess is the E column)
    BytDataColumnMakesIDInternalColumn = 2          'Insert here the internal reference of the MakesID in the data sheet for the VLOOKUP functions (since it's in the second column, i set it to 2)
    Set RngCellWithDropDown = Sheets(StrImportSheetName).Range("B3") 'Insert here the cell on witch you are going to apply the validation dropdown.

    'Setting validation.
    With RngCellWithDropDown.Validation
        .Delete
        .Add Type:=xlValidateList, _
             AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, _
             Formula1:="=INDIRECT(""" & StrDataSheetName & "!" & StrDataColumnSeries & """&MATCH(VLOOKUP(" & StrImportColumnMake & RngCellWithDropDown.Row & "," & StrDataSheetName & "!" & StrDataColumns & "," & BytDataColumnMakesIDInternalColumn & ",FALSE)," & StrDataSheetName & "!" & StrDataColumnSeriesParentIDEntire & ",0)&"":" & StrDataColumnSeries & """&COUNTIF(" & StrDataSheetName & "!" & StrDataColumnSeriesParentIDEntire & ",VLOOKUP(" & StrImportColumnMake & RngCellWithDropDown.Row & "," & StrDataSheetName & "!" & StrDataColumns & "," & BytDataColumnMakesIDInternalColumn & ",FALSE))+MATCH(VLOOKUP(" & StrImportColumnMake & RngCellWithDropDown.Row & "," & StrDataSheetName & "!" & StrDataColumns & "," & BytDataColumnMakesIDInternalColumn & ",FALSE)," & StrDataSheetName & "!" & StrDataColumnSeriesParentIDEntire & ",0)-1)"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With

End Sub

Как я уже сказал, это должно работать до тех пор, пока данные сортируются по идентификатору родителя серии. Скажите, если вам нужно применить его на нескольких ячейках. Я могу редактировать код соответственно. Также, если вам нужно какое-либо объяснение по поводу действительно грязной формулы, просто скажите, пожалуйста.

...