Hi all,
I have workbook that I would like to automatically hide/unhide rows & columns based on values.
I have 3 activation cells on a worksheet called "Capacity Requirements".
I have been using the code below within the worksheet and it works great.
As my workbook has grown over time, my model requires more worksheets. I would now like my activation cells (D7, C34, C39, and C44) on the "Capacity Requirements" worksheet to automatically hide/unhide rows and columns on other worksheets through the workbook.
For example, when the model term is set to 6, I would like to hide any columns that refer to 2022. On my "Results" worksheet, I have formulas that return 1 or 0 in cells K2:Q2 (1 column per year). When 6 is selected in cell D7 on the "Capacity Requirements" worksheet column Q on the "Results" worksheet should automatically be hidden and Columns K, L, M, N and O should be unhidden if they are currently hidden.
I have a number of other rows/columns in different worksheets that should also be hidden/unhidden based on the values in cells D7, C34, C39, and C44 on the "Capacity Requirements" worksheet.
Any help on this is greatly appreciated.
Thanks,
Adam1988
I have workbook that I would like to automatically hide/unhide rows & columns based on values.
I have 3 activation cells on a worksheet called "Capacity Requirements".
- D7: The value of that cell can be 1-7 and is intended to determine the term of the model (from 2016-2022). In row 2, I have formulas in cells E2:R2 that will either return a 1 or 0 based on if that year should be included in the mode (there are two columns for each year - 2016 is in columns E & F). If the value 0 is returned, then the column should be hidden and if the value 1 is returned the column should be unhidden.
- C34: This cell can either be blank or have some text in it. If it is blank my formula in cells A35:A38 will return 0 (these rows should be hidden), if there is text in C34 the formulas will return 1 (row 35:38 should be unhidden)
- C39: This cell can either be blank or have some text in it. If it is blank my formula in cells A40:A43 will return 0 (these rows should be hidden), if there is text in C39 the formulas will return 1 (row 40:43 should be unhidden)
- C44: This cell can either be blank or have some text in it. If it is blank my formula in cells A45:A48 will return 0 (these rows should be hidden), if there is text in C44 the formulas will return 1 (row 45:48 should be unhidden)
I have been using the code below within the worksheet and it works great.
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$D$7" Then
Application.ScreenUpdating = False
For i = 5 To 18
If Cells(2, i) = 0 Then
Columns(i).EntireColumn.Hidden = True
Else
Columns(i).EntireColumn.Hidden = False
End If
Next i
Application.ScreenUpdating = True
End If
If Target.Address = "$C$34" Then
Application.ScreenUpdating = False
For j = 35 To 38
If Cells(j, 1) = 0 Then
Rows(j).EntireRow.Hidden = True
Else
Rows(j).EntireRow.Hidden = False
End If
Next j
Application.ScreenUpdating = True
End If
If Target.Address = "$C$39" Then
Application.ScreenUpdating = False
For k = 40 To 43
If Cells(k, 1) = 0 Then
Rows(k).EntireRow.Hidden = True
Else
Rows(k).EntireRow.Hidden = False
End If
Next k
Application.ScreenUpdating = True
End If
If Target.Address = "$C$44" Then
Application.ScreenUpdating = False
For l = 45 To 48
If Cells(l, 1) = 0 Then
Rows(l).EntireRow.Hidden = True
Else
Rows(l).EntireRow.Hidden = False
End If
Next l
Application.ScreenUpdating = True
End If
End Sub
As my workbook has grown over time, my model requires more worksheets. I would now like my activation cells (D7, C34, C39, and C44) on the "Capacity Requirements" worksheet to automatically hide/unhide rows and columns on other worksheets through the workbook.
For example, when the model term is set to 6, I would like to hide any columns that refer to 2022. On my "Results" worksheet, I have formulas that return 1 or 0 in cells K2:Q2 (1 column per year). When 6 is selected in cell D7 on the "Capacity Requirements" worksheet column Q on the "Results" worksheet should automatically be hidden and Columns K, L, M, N and O should be unhidden if they are currently hidden.
I have a number of other rows/columns in different worksheets that should also be hidden/unhidden based on the values in cells D7, C34, C39, and C44 on the "Capacity Requirements" worksheet.
Any help on this is greatly appreciated.
Thanks,
Adam1988