excel 根据用户输入隐藏列

bttbmeg0  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(110)

我试图建立一个20名员工和2年的计划。在这里,我想隐藏的几个星期已经通过了输入。
下面是我得到了多少。经过测试,它似乎工作,直到“所有”部分,但不能弄清楚最后一部分。与输入框,我希望的人能够输入的值基于第一行,2023周1(20231)到2024周52(202452)。
我的目标是,如果有人输入202336的值,它应该显示2023年第36周及以后,并隐藏之前的几周。
有人能帮我吗?先谢谢了!

Sub Hidepastweeks()
    Dim myValue As Variant
    myValue = InputBox("Weeks visible from week:", "Visable weeks")
    Dim c As Range
    
    Range("A2").Value = myValue
    With Range("G1:DF1")
        Application.ScreenUpdating = False
        .EntireColumn.Hidden = (myValue <> "All")
        If myValue <> "All" Then
            For Each c In Range("G1:DF1").Cells
                If c.Value < "myValue" Then
                    c.EntireColumn.Hidden = True
                End If
            Next
        End If
        Application.ScreenUpdating = True
    End With
End Sub

我尝试了不同的网站,但他们似乎都有一个等于公式,而不是较小,然后。

ffvjumwh

ffvjumwh1#

匹配前隐藏列(Application.Match

Sub HidePastWeeks()
    
    Dim myValue As Variant
    myValue = InputBox("Weeks visible from week:", "Visible weeks")
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    ws.Range("A2").Value = myValue
    
    Dim srg As Range: Set srg = ws.Range("G1:DF1")
    
    ' Allow case-insensitivity with 'vbTextCompare' i.e. 'all = ALL'.
    Dim ItsNotAll As Boolean
    ItsNotAll = StrComp(myValue, "All", vbTextCompare) <> 0
    
    Application.ScreenUpdating = False
        
    ' Show or hide all columns if it's 'All' or not 'All' respectively.
    srg.EntireColumn.Hidden = ItsNotAll
    
    Dim cIndex As Variant ' could be an error value
    
    If ItsNotAll Then
        cIndex = Application.Match(myValue, srg, 0)
        If IsNumeric(cIndex) Then ' show
            srg.Resize(, srg.Columns.Count - cIndex + 1) _
                .Offset(, cIndex - 1).EntireColumn.Hidden = False
        'Else ' show none; previously already set
        End If
    'Else ' show all; previously already set
    End If
    
    Application.ScreenUpdating = True

End Sub
2wnc66cl

2wnc66cl2#

如果去掉myValue周围的引号,它可能会按原样工作
如果c.值〈“myValue”,则
所写的周数是否与您从用户输入中请求的相同,例如202336在CG1(或其他)中?如果不是,您必须使用adjust才能与myValue进行比较。

相关问题