当前位置: 首页 > 知识库问答 >
问题:

使用VBA的从属下拉列表

吴鸿彩
2023-03-14

如果c.value=activeSheet.range(“A2:A10000”)。value然后“selected GrantNumber”,则在以下位置出现类型不匹配错误:

任何帮助都很感激。谢谢

Sub SetupGrantNumber() 'run this on workbook open event
    Dim rng As Range
    Set rng = Worksheets("IOHealthcareLinkageTemplate").Range("A2:A10000")  'choose your cell(s) here
    With rng.Validation
        FRM = GetUniqueGrantNumbers()
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=FRM
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub


Sub SetupIOName()  'run this sub on the change event of GrantNumber cell
    Dim rng As Range
    Set rng = Worksheets("IOHealthcareLinkageTemplate").Range("B2:B10000")  'choose your cell(s) here
    With rng.Validation
        FRM = GetIONames()
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=FRM
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub


Function GetUniqueGrantNumbers() As String
    Dim sOut As String
    Dim v, c
    Dim rngList As Range

    Set rngList = Worksheets("IOs").Range("A2:A10000") 'edit the range where your GrantNumber list is stored
    sOut = ""

    For Each c In rngList
        If InStr(1, sOut, c.Value & ",") = 0 Then  'check if the value is already in the upload list and add if not there
            sOut = c.Value & "," & sOut
        End If
    Next c
    'remove first ,
    If sOut <> "" Then
        sOut = Left(sOut, Len(sOut) - 1)
    End If
    GetUniqueGrantNumbers = sOut
End Function


Function GetIONames() As String
    Dim sOut As String
    Dim v, c
    Dim rngSearch As Range

    Set rngSearch = Worksheets("IOs").Range("C2:C10000") 'edit the range where  your IOname list exists
    sOut = ""

    For Each c In rngSearch
        If c.Value = ActiveSheet.Range("A2:A10000").Value Then 'selected GrantNumber
            sOut = sOut & "," & ActiveSheet.Range("E" & c.Row).Value
        End If
    Next c
    'remove first ,
    If sOut <> "" Then
        sOut = Mid(sOut, 2)
    End If
    GetIONames = sOut
End Function

共有1个答案

阎德宇
2023-03-14

请将以下代码放在thisworkbook中。SetupGrantNumber还可以通过手动或按按钮或其他方式启动,因为它收集所有grantnumbers以用于A列中的数据验证:

Private Sub Workbook_Open()
    Call SetupGrantNumber
End Sub

两个直接相关的子模块可以放在一个模块中:

Sub SetupGrantNumber()
    FRM = GetUniqueGrantNumbers()
    If FRM <> "" Then
        With Worksheets("IOHealthcareLinkageTemplate").Range("A2:A10000").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=FRM
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
    End If
End Sub

Function GetUniqueGrantNumbers() As String
    Dim sOut As String
    Dim c As Range
    sOut = ""
    With Worksheets("IOs")
        For Each c In .Range("A2:A10000")
            If InStr(1, sOut, c.Value & ",") = 0 Then
                sOut = c.Value & "," & sOut
            End If
        Next c
    End With
    If sOut <> "" Then
        sOut = Left(sOut, Len(sOut) - 1)
    End If
    GetUniqueGrantNumbers = sOut
End Function

下面的代码也必须放在“ThisWorkbook”中,因为它会自动检查范围A:A中的任何单元格是否发生了更改。然后Excel自动使用更改的单元格值运行SetupIOName:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim MonitoredCells As Range
    Dim c As Range
    If Sh.Name = "IOHealthcareLinkageTemplate" Then
        Set MonitoredCells = Intersect(Target, Target.Parent.Range("A:A"))
        If Not MonitoredCells Is Nothing Then
            For Each c In MonitoredCells
                If c.Value <> "" Then SetupIOName (c.Value)
            Next c
        End If
    End If
End Sub
Sub SetupIOName(ByRef SelectedGrantNumber As String)
    FRM = GetIONames(SelectedGrantNumber)
    If FRM <> "" Then
        With Worksheets("IOHealthcareLinkageTemplate").Range("B2:B10000").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=FRM
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
    End If
End Sub

Function GetIONames(ByRef SelectedGrantNumber As String) As String
    Dim sOut As String
    Dim c As Range
    sOut = ""
    With Worksheets("IOs")
        For Each c In .Range("A2:A10000")
            If c.Value = SelectedGrantNumber Then
                sOut = sOut & "," & .Cells(c.Row, "C").Value
            End If
        Next c
    End With
    If sOut <> "" Then
        sOut = Mid(sOut, 2)
    End If
    GetIONames = sOut
End Function
 类似资料:
  • 嗨Stackoverflow社区, 如果满足条件,我想添加一个数据验证下拉列表。 如果“最终侦察”出现在C列中,则以以下状态填充下拉列表:“最终”或“正在审查”,否则不要填充下拉列表。 我可以使用非VBA验证列表,但在这种类型的评估审查中,我需要使用VBA版本。 这就是我到目前为止所拥有的,我被困得很厉害。我在C栏有“最终侦察”,如果最终侦察出现在C栏,然后用“最终”和“正在审查”的选项填充下拉列

  • 问题内容: 我想创建一个包含国家/地区的下拉列表,并创建一个包含城市的第二个下拉列表,这取决于第一个列表中的选定值。并且城市列表应动态更改。在视图(Thymeleaf)中,我有一个from控制器。CountryModel的名称应该显示在第二个下拉列表中,而Set应该显示在第二个(相关的)下拉列表中。 在这里,我创建第一个下拉列表: 那么如何创建第二个下拉列表,该列表取决于第一个列表中的所选国家/地

  • 在这里,我想在动态下拉列表中设置一个属性“selected”,当它在MVC中的HttpPost之后被单击时,它会显示相同的第一个文本“请选择一个类别”。 从HttpPost重载后,我想通过使用值来检查和设置选项标签中的属性“选择”,在Foreach循环选项标签中的动态下拉中获得“选择”属性。

  • 我们在基于java的web应用程序中有一个功能,用户可以从web应用程序下载excel表模板。在此模板中填写他们的数据,然后上载相同的excel工作表。 然后系统读取此excel文件并将此数据保存到数据库中。 下面是模板文件的快照,其中有一些示例数据。 我想要的是,当用户下载模板文件(模板文件通常只有标题,所以用户知道哪些数据在哪一列)时,excel表应该有部门、产品、二级产品、地区和国家的下拉列

  • Im使用VBA代码创建多个选择下拉列表。该代码将使目标单元格中的每个下拉列表变成多重选择列表,函数为: 目标单元格中下拉列表的来源是,b14是另一个下拉列表(单选)。现在,如果b14的值将变成list1,我想让目标的单元格列表变成多个选择列表。在任何其他情况下,我希望它工作在正常的excel方式。我尝试用来precaphy列表源,但是我得到了的不匹配错误。我怎么做?

  • 我需要使用Apache POI在excel文件中创建一个下拉列表。我能够做到这一点,但我不能使下拉列表中的第一个项目作为默认项目。