通过双条件填充列表

发布于 2024-08-13 17:33:41 字数 557 浏览 1 评论 0原文

我有兴趣实施一些东西,但我不确定它是否可能,并且希望您能参与其中。

这是我的场景:

我将有两个验证单元,它们将显示为我将从中进行选择的列表。 这些是我想要满足的条件并从数据库中删除我的列表。

我有一个要抛出 B13:B23 的特工列表,接下来的两个我有数据列 假设我的数据库看起来像这样

 B     C      D     E
       X  |   Y  |  Z
agent1 1  |   1  |  0
agent2 0  |   1  |  0
agent3 0  |   1  |  1
agent4 1  |   0  |  0

...

当我从验证单元格1:X和验证单元格2:1中选择时,我想从B列填充代理名称列表。 它应该只显示

column:
agent1
agent4

X 列中带有 0 的或代理...

我在某处读过有关数组公式的内容,但我不知道这是否方便,不幸的是我没有任何宏背景:(但我知道在 C++ 中这样的东西相当容易 ,

预先感谢

im interested to implement something but im not sure if it would be possible and would like your intake on it.

here is my scenario:

i will have two validation cells which will be shown as List that I will pick from.
those are my condition that i would like to meet and ommit my list from a database.

I have a list of agents going threw B13:B23 and next two them i have columns of data
assuming that my data base looks like this

 B     C      D     E
       X  |   Y  |  Z
agent1 1  |   1  |  0
agent2 0  |   1  |  0
agent3 0  |   1  |  1
agent4 1  |   0  |  0

...

i want to populate a list of agent name from column B, when i select from the validation cell1: X and validation cell2: 1.
it should show only

column:
agent1
agent4

or agents in column X with 0...

i read somewhere about array formula but i dont know if this is convenient and i unfortunately dont have any background in macros:( but i know in C++ something like this is fairly easy with conditional statements.

thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2024-08-20 17:33:41

这是可能的。解决此问题的一种方法是,每当 Worksheet_Change 中的目标与验证单元格 1 或单元格 2 相交时,调用一个列出相关代理的子程序 -> 。验证发生了变化。

然后,您将运行一个具有 3 个参数 srcRange、validationColumn 和validationValue 的子程序,该子程序遍历 srcRange 的每一行,并检查位置 rownumber、validationColumn 上的单元格是否等于validationValue,如果是,则输出代理并设置 outputrow + 1

将此 VBA 放入你的工作表:

Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim watchRange As Range
        Dim validationValue As Range
        Dim validationColumn As Integer
        Set watchRange = Me.Range("H1, I1") ' Validation Cells '

        If Not Intersect(Target, watchRange) Is Nothing Then
            Set validationValue = Me.Range("I1")
            validationColumn = 0
            With Me.Range("H1")
                If (.value = "X") Then validationColumn = 2
                If (.value = "Y") Then validationColumn = 3
                If (.value = "Z") Then validationColumn = 4
            End With
                listAgents Me.Range("B3:E6"), validationColumn, validationValue
        End If

    End Sub

    Private Sub listAgents(ByRef srcRange As Range, ByVal validationColumn As Integer, ByRef validationValue As Range)

        Dim outputStart As Range
        Dim row As Range
        Dim i As Long

        Set outputStart = Me.Range("H3")
        outputStart.CurrentRegion.Clear

        If validationColumn = 0 Then
            MsgBox "Can't find Validation Column"
            Exit Sub
        End If

        i = 0
        For Each row In srcRange.Rows
            If (row.Cells(1, validationColumn) = validationValue) Then
                outputStart(1 + i, 1) = row.Cells(1, 1)
                i = i + 1
            End If
        Next row
    End Sub

我在你的例子上测试了它并且它有效。

It is possible. One way to approach this is to call a sub that lists you the relevant agents whenever the Target in your Worksheet_Change intersects with validation cell1 or cell2 -> the validation changed.

Then you would run a sub with 3 paramenters srcRange, validationColumn and validationValue that goes throgh each row of srcRange and checks if the cell on position rownumber, validationColumn is equal to validationValue if so it outputs the agent and sets outputrow + 1

Put this VBA in your Sheet:

Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim watchRange As Range
        Dim validationValue As Range
        Dim validationColumn As Integer
        Set watchRange = Me.Range("H1, I1") ' Validation Cells '

        If Not Intersect(Target, watchRange) Is Nothing Then
            Set validationValue = Me.Range("I1")
            validationColumn = 0
            With Me.Range("H1")
                If (.value = "X") Then validationColumn = 2
                If (.value = "Y") Then validationColumn = 3
                If (.value = "Z") Then validationColumn = 4
            End With
                listAgents Me.Range("B3:E6"), validationColumn, validationValue
        End If

    End Sub

    Private Sub listAgents(ByRef srcRange As Range, ByVal validationColumn As Integer, ByRef validationValue As Range)

        Dim outputStart As Range
        Dim row As Range
        Dim i As Long

        Set outputStart = Me.Range("H3")
        outputStart.CurrentRegion.Clear

        If validationColumn = 0 Then
            MsgBox "Can't find Validation Column"
            Exit Sub
        End If

        i = 0
        For Each row In srcRange.Rows
            If (row.Cells(1, validationColumn) = validationValue) Then
                outputStart(1 + i, 1) = row.Cells(1, 1)
                i = i + 1
            End If
        Next row
    End Sub

I tested it on your example and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文