隐藏日期范围以外的多列

时间:2018-11-07 13:51:08

标签: excel vba excel-vba

我正在尝试编写VBA代码以仅显示两个日期之间的某些列,这些列在其他单元格中输入。由于Excel不会将“日期1”(讨厌)作为所有合并单元格的内容,因此无法使用某些“ while”逻辑,这在这里可能很棒。

我已经尝试了一些方法,但是它们显然不起作用。

上下文: 目前,我在工作簿的多个工作表中都包含此信息,我希望将所有信息都包含在一个工作表中,而要使其井井有条的想法是使用过滤器。

如果有人对代码有想法,或以其他方式组织它,那将非常有帮助。

我尝试的代码:

With ActiveSheet
    LastDate = .Cells("2", Columns.Count).End(xlToLeft).Column
    Set DDS = Range(.Cells(2, 11), .Cells(56, LastDate))
End With

For i = 1 To LastDate
    If DDS(1, i) = StartDate Then
        DDS.Cells(1, i).EntireColumn.Hidden = False
        i = i + 22
        For j = i To LastDate
            If DDS(1, j) = EndDate Then
                For k = j To LastDate
                    If DDS(1, k) <> 0 And DDS(1, k) <> EndDate Then
                        j = k
                        Exit For
                    End If
                Next k
                i = j
                Exit For
            End If
        Next j
    Else
        DDS.Cells(1, i).EntireColumn.Hidden = True
        i = i + 22
    End If
Next i

Tables

谢谢!

1 个答案:

答案 0 :(得分:0)

也许类似于以下内容:

Function ShowDate(StartDate As Date, LastDate As Date)

Dim tVar As Variant, CurDate As Date, hideBoolean As Boolean

With ActiveSheet
    ' Get the date range, and the field ranges, and put into an array for easy manipulation
    tVar = Intersect(.UsedRange, .Range("2:3"))
End With

' For each column
For i = 1 To UBound(tVar, 2)
    ' If there is no field, then let's not do anything
    If Len(tVar(2, i)) = 0 Then GoTo SkipIt
    ' If there is a date, then make a note of it, otherwise make the top level equal to the current date
    If Not Len(tVar(1, i)) = 0 Then CurDate = tVar(1, i) Else tVar(1, i) = CurDate
    ' Figure out whether we want to hide the column or not.
    hideBoolean = (tVar(1, i) < StartDate) And (tVar(1, i) > LastDate)
    ' And finally hide or unhide that column
    ActiveSheet.Cells(1, i).EntireColumn.Hidden = hideBoolean
SkipIt:
    Next i

End Function
相关问题