在VBA(Excel)中执行过滤器后运行宏

时间:2017-07-04 21:58:25

标签: excel vba excel-vba autofilter

我需要在更改列的顺序后运行宏。

详细说明: 从大型数据库中提取一些列到精简表。从数据库“只是复制”之后,精简表执行一些宏来根据日期绘制几个单元格。事情是第一次,它工作正常,但是当我用自动过滤器安排单元格(例如按顺序设置日期)时,我需要再次执行单元格绘制宏。我能够弄清楚的是,当您执行自动过滤器选择时,数据不会“改变”。我该怎么办?

这是我到现在为止的代码......

每次应用过滤器时,“$ D $ 9”单元格都应该更改,因为此单元格包含数据库中原始行的编号

提前感谢您的帮助。

AGUS

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$D$9" Then

        Call plandesplegado

    End If

End Sub

修改

plandesplegado已添加

Sub plandesplegado()

Dim i As Integer
Dim j As Integer
Dim fecha As Date
Dim semana As Integer
Dim anio As Integer

For i = 9 To lastrow + 8

    For j = 19 To 71

        If Cells(i, 12) <> "No aplica" Or Cells(i, 12) = "" Then

            fecha = Cells(i, 12)
            anio = year(fecha)

                If anio = Cells(5, 17) Then

                        Cells(i, 17) = "C"

                ElseIf anio = Cells(5, 73) Then

                        Cells(i, 73) = "C"

                Else

                    semana = WorksheetFunction.WeekNum(fecha, 1)

                        If semana = j - 18 Then

                            Cells(i, j) = "C"

                        End If

                End If

        End If

    Next j

Next i

For i = 9 To lastrow + 8

    For j = 19 To 71

        If Cells(i, 15) <> "No aplica" Or Cells(i, 15) = "" Then

            fecha = Cells(i, 15)
            anio = year(fecha)

                If anio = Cells(5, 17) Then

                        Cells(i, 17) = "P"

                ElseIf anio = Cells(5, 73) Then

                        Cells(i, 73) = "P"

                Else

                    semana = WorksheetFunction.WeekNum(fecha, 1)

                        If semana = j - 18 Then

                            Cells(i, j) = "P"

                        End If

                End If

        End If

    Next j

Next i

For i = 9 To lastrow + 8

    For j = 19 To 71

        If Cells(i, 13) <> "No aplica" Or Cells(i, 13) = "" Then

            fecha = Cells(i, 13)
            anio = year(fecha)

                If anio = Cells(5, 17) Then

                        Cells(i, 17) = "V"

                ElseIf anio = Cells(5, 73) Then

                        Cells(i, 73) = "V"

                Else

                    semana = WorksheetFunction.WeekNum(fecha, 1)

                        If semana = j - 18 Then

                            Cells(i, j) = "V"

                        End If

                End If

        End If

    Next j

Next i

End Sub

修改

工作簿的图片:http://tinypic.com/r/v6lxtu/9

0 个答案:

没有答案