如何根据月份VBA excel

时间:2015-10-20 13:04:49

标签: excel vba excel-vba dynamic

我正在编写一个代码来隐藏传递当前月份(IntMonth)的数据。因此,例如,如果在IntMonth中选择了8,则应隐藏直到12日的第9个月。我尝试了不同的东西,但我无法设法做出一致的宏。公平地说,我对VBA很新。

编码目标

  • 代码应隐藏并分组通过当前日期的所有月份,填写为IntMonth。因此,如果为IntMonth填写了8,则代码应该分组并隐藏第9-12个月,并且应该能够在手动调整月份时重复此过程。
  • 从第5张纸开始一直到第100张
  • 编码应跳过以" CC"
  • 开头的表格

到目前为止我写的代码下面,这段代码有以下缺陷

当前编码错误

  • 现在代码不包括最后一张
  • 编码应该停止进入单词"结束" (第N行)第5行但总是包括下一列(Colomn T)
  • 代码仅在您运行两次时才有效

Colomn lay-out

            Sub Groeperen()


            '----------------------------------------------------------------------------------------------------
            'Update variables from this point

            Dim IntMonth As Integer, rData As Range, rCel As Range, x As Integer, i As Long

              IntMonth = Range("IntMonth")

            '----------------------------------------------------------------------------------------------------
            'Define sheetnumber where to start running the macro (as i)
                x = 5

            '----------------------------------------------------------------------------------------------------
            'Don't change anything after this point
            '----------------------------------------------------------------------------------------------------

             Application.ScreenUpdating = False

            'ActiveWorkbook.RefreshAll

            For i = x To Worksheets.Count
                Worksheets(i).Select
                If Right(ActiveSheet.Name, 2) <> "CC" Then

                    Range("C5").Select
                    Set rData = Range(ActiveCell, ActiveCell.End(xlToRight))


                    Range(Columns("A"), Selection.End(xlToRight)).Select
                    On Error Resume Next
                    Selection.Columns.Ungroup
                    On Error Resume Next
                    Selection.EntireColumn.Hidden = False
                    Range("A1").Select
                        For Each rCel In rData
                            If rCel >= IntMonth Then
                                Columns(rCel.Column).Group
                                rCel.EntireColumn.Hidden = True
                            Else
                                If rCel = "End" Then Stop
                            End If
                        Next 'rCel


                End If 'Right(Sheets(i).Name, 2) <> "CC"

            Next 'i

              Application.ScreenUpdating = True

            End Sub

1 个答案:

答案 0 :(得分:0)

我根据你写的内容清理了For Next块及其中的所有内容,调整代码以完成你所追求的目标。我修复了错误的语法,以及逻辑。你非常接近在那里,你只是有一点逻辑。

For i = x To Worksheets.Count

    Dim ws As Worksheet
    Set ws = Worksheets(i)

    With ws

        If Left(.Name, 2) <> "CC" Then

            .Activate

            Set rData = .Range(.Range("C5"), .Range("N5")) 'can set to N5 since it's constant and it will stop before it gets to "End"
            With .Range(.Range("A5").EntireColumn, .Range("N5").End(xlToRight))
                On Error Resume Next
                .Columns.Ungroup
                On Error GoTo 0
                .EntireColumn.Hidden = False
            End With

            For Each rCel In rData
                If rCel >= IntMonth Then
                    .Columns(rCel.Column).Group
                    rCel.EntireColumn.Hidden = True
                End If
            Next 'rCel

        End If 'Left(ws.Name, 2) <> "CC" Then

    End With

Next 'i