1

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,

4

1 回答 1

2

有可能的。解决此问题的一种方法是,只要 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

我在您的示例中对其进行了测试,并且有效。

于 2009-12-04T21:30:35.950 回答