Dim score As Variant
score = Range("KPI!A6").Value
Select Case score
Case score = "January"
Columns("D:N").EntireColumn.Hidden = True
Case score = "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case score = "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case score = "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case score = "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case score = "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case score = "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case score = "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case score = "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case score = "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case score = "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case score = "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
嘿所有人!这是我的第一个问题,所以我希望我不会做错事。在任何情况下(双关语都在这里)....我有一些相对基本的"案例"代码应该查看A6的单元格值并根据显示的月份隐藏列。我很难弄清楚为什么它会跳过正确的情况(在这种情况下" May")。我可以单步执行代码,但它不会停止执行隐藏列部分。它只是继续前进,检查每个案例并最终击中子的结尾并结束。我没有收到任何错误,它只是没有捕获正确的值并执行操作。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:4)
如果score
是January
之类的字符串,那么score = "January"
(等)将返回True
(或False
)。 True
(或False
)不等于score
的值(即"January"
),因此您的测试都不会得到满足。
你可能想说:
Dim score As Variant
score = Range("KPI!A6").Value
Select Case score
Case "January"
Columns("D:N").EntireColumn.Hidden = True
Case "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
将依次将score
的值与每个文字字符串"January"
,"February"
等进行比较,并执行比较为{Case
的{{1}} {1}}。
或者,正如Jeeped建议的那样,为了避免从您可以使用的每个True
语句中删除score =
:
Case
会将值Dim score As Variant
score = Range("KPI!A6").Value
Select Case True
Case score = "January"
Columns("D:N").EntireColumn.Hidden = True
Case score = "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case score = "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case score = "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case score = "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case score = "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case score = "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case score = "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case score = "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case score = "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case score = "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case score = "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
依次与True
,score = January
等每个逻辑表达式进行比较,并执行第一个score = February
比较Case
会返回True
。
True
语句只是一个漂亮的Select Case
语句。以下If
声明
Select Case
等同于以下Select Case x
Case y
DoSomething
Case z
DoSomethingElse
End Select
语句
If
答案 1 :(得分:4)
无需选择案例:
Dim score As Variant
With Worksheets("KPI")
score = .Range("A6").Value
'if score is a date remove this next line
score = DateValue(1 & " " & score & " 2017")
.Columns("C:N").Hidden = True
.Columns(Month(score) + 2).Hidden = False
End With