在隐藏列时无法弄清楚如何包含所有工作表

时间:2018-02-12 21:54:55

标签: excel vba excel-vba

大约12个小时后我终于放弃了。我终于想出了如何正确地隐藏列,或者至少我认为它正常工作。欢迎任何反馈/更正。

有人能告诉我如何为所有工作表(一周中的每一天)而不仅仅是一个工作表吗?我再也找不到了!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim ws As Worksheet
Dim entireRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Fri")

Set entireRange = ws.Columns("AI:AN")

    entireRange.EntireColumn.Hidden = False

Select Case ActiveSheet.Range("C4")

  Case "6"
      ws.Range("AJ:Am").EntireColumn.Hidden = True

  Case "7"
      ws.Range("AK:AM").EntireColumn.Hidden = True

  Case "8"
      ws.Range("AL:AM").EntireColumn.Hidden = True

  Case "9"
      ws.Range("AM:AM").EntireColumn.Hidden = True

  Case "5"
      entireRange.Hidden = True

End Select
End Sub

感谢您的帮助,以及您在这里给予所有其他人的所有帮助,使我能够做到这一点。如果有帮助,这是文件:https://drive.google.com/open?id=1sSGSMCAo-4JrvLF0s81MxjC4dZ78RgPb

2 个答案:

答案 0 :(得分:0)

创建您的日期数组并循环显示工作表:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim ws As Worksheet
Dim entireRange As Range
Set wb = ThisWorkbook
Dim day as string

Days = Array("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun")

For each day in Days
Set ws = wb.Sheets(day)
Set entireRange = ws.Columns("AI:AN")

entireRange.EntireColumn.Hidden = False

Select Case ActiveSheet.Range("C4")

    Case "6"
       ws.Range("AJ:Am").EntireColumn.Hidden = True

    Case "7"
      ws.Range("AK:AM").EntireColumn.Hidden = True

   Case "8"
       ws.Range("AL:AM").EntireColumn.Hidden = True

   Case "9"
       ws.Range("AM:AM").EntireColumn.Hidden = True

    Case "5"
     entireRange.Hidden = True
End Select
Next Day

End Sub

答案 1 :(得分:0)

假设您希望在工作表(而不仅仅是“Fri”)上更改单元格C4时隐藏自动列,则下面的代码应该可以正常运行。希望C4上有数据验证。

可能的改进:隐藏的范围似乎可以通过C4中的值计算,而不是由每个值固定。

['my_python_app.py', '-u', '-t', 'tar', '-p', '-f']

<小时/> 更新:获取Excel文件并了解目标是什么后,这将非常有用。轻松添加更多影院 - 更改Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet Dim entireRange As Range, hideRange As Range ' Assuming only monitor changes on cell C4 on the worksheet, not just "Fri" If Target.Address(0, 0) <> "C4" Then Exit Sub Application.ScreenUpdating = False Set ws = Target.Worksheet Set entireRange = ws.Columns("AI:AN") entireRange.EntireColumn.Hidden = False Select Case CInt(Target.Value) Case 6 Set hideRange = ws.Range("AJ:AM") Case 7 Set hideRange = ws.Range("AK:AM") Case 8 Set hideRange = ws.Range("AL:AM") Case 9 Set hideRange = ws.Range("AM:AM") Case Else ' 5 Set hideRange = entireRange End Select hideRange.EntireColumn.Hidden = True Set entireRange = Nothing Set hideRange = Nothing Set ws = Nothing Application.ScreenUpdating = True End Sub 文字。

TheatersColumns