Office中国论坛/Access中国论坛

标题: 大家根据人员名册中的相关条件提取人员姓名 [打印本页]

作者: 何必啊    时间: 2013-8-4 09:40
标题: 大家根据人员名册中的相关条件提取人员姓名
大家帮我看看,要实现这个目的是不是要用到VBA

作者: tmtony    时间: 2013-8-4 10:01
复杂的条件用VBA会更灵活!
作者: tmtony    时间: 2013-8-4 10:02
复杂的条件用VBA会更灵活!
作者: pureshadow    时间: 2013-8-4 10:47
这种带合并单元格的,用VBA比用函数简单
作者: todaynew    时间: 2013-8-4 12:06
本帖最后由 todaynew 于 2013-8-4 12:10 编辑


[attach]52250[/attach]

[attach]52249[/attach]

Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target
        Case Cells(2, 6)
            Call 遍历数据(Target, 1)
        Case Cells(4, 6)
            Call 遍历数据(Target, 2)
        Case Cells(6, 6)
            Call 遍历数据(Target, 3)
    End Select
End Sub


Private Sub 遍历数据(ByVal Target As Range, ByVal m As Integer)
    Dim r As Range
    Dim Area1 As Range, Area2 As Range
    Dim i As Integer
    Dim x1 As Integer, y1 As Integer
    Dim x2 As Integer, y2 As Integer
   
    Set r = Range("Data")   '数据区域
    Set Area1 = Range("Area" & m & "1") '党员区域
    Set Area2 = Range("Area" & m & "2") '团员区域
    x1 = 1: y1 = 1
    x2 = 1: y2 = 1
    For i = 1 To r.Rows.Count
        If r.Cells(i, 1).Text = Target.Text Then
            Select Case r.Cells(i, 3).Text
                Case "党员"
                    Area1.Cells(x1, y1) = r.Cells(i, 2).Text
                    y1 = y1 + 1
                    If y1 > Area1.Columns.Count Then
                        x1 = x1 + 1
                        y1 = 1
                    End If
                Case "团员"
                    Area2.Cells(x2, y2) = r.Cells(i, 2).Text
                    y2 = y2 + 1
                    If y2 > Area2.Columns.Count Then
                        x2 = x2 + 1
                        y2 = 1
                    End If
            End Select
        End If
    Next
End Sub



作者: 何必啊    时间: 2013-8-10 19:11
谢谢,真是高人!
这个可不可以用函数来实现?

作者: todaynew    时间: 2013-8-13 17:38
何必啊 发表于 2013-8-10 19:11
谢谢,真是高人!
这个可不可以用函数来实现?

不知道是否可用函数实现,不过用vb代码不是很简单吗?




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3