I have an Excel workbook with 2 sheets: summary
and ClickHide
.
Summary Sheet
contains multiple checkbox forms. When the checkbox is checked, they return a TRUE
or FALSE
value in the ClickHide Sheet
, all in ROW A
.
ClickHideSheet
: Next to these values in A2:A20
, I attribute a "0" or "1" value to the TRUE or FALSE
result. These results go horizontally in range B2:BZ45
. On ClickHide Row 1
, I have formulae to get a result on top of each column.
In Summary B1:BZ1
I have copied links to the cells in ClickHide A1:BZ1
.
My question :
I intend to make the columns in
Summary
to automatically hide when the value in their own row is not 0. (if B1 is not 0, hide column B, otherwise unhide). The user will interact with the checkboxes, so the macro needs to be triggered every time a checkbox is modified.
I have found code snippets out there, but none of them seem to work. I'm really bad at VBA, so it could be a simple edit that I'm missing.
I've tried both of these, with no success. Have a look the following reference .
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 80
Cells(1, i).EntireColumn.Hidden = Cells(2, i) = 0
Next
End Sub
This next one I don't really know if it's a VBA code or not.
oSheets = ThisComponent.getSheets()
oSheet1 = oSheets.getByName("Sheet1")
oColumns = oSheet1.getColumns()
for i = 0 to 25
oCol = oColumns.getByIndex(i)
oCell = oSheet1.getCellByPosition(i,0) 'First row has index 0
If oCell.Value = 0 Then
oCol.isVisible = False
Else
oCol.isVisible = True
End If
next i
I don't know how to embed a spreadsheet in here, so here are pictures of both sheets, simplified to show an example.
Thanks in advance for any help you can provide.
The codes that you posted don't belong to Excel rather it is of openoffice.
Below code will hide columns for the cells(in 1st row) that have values =0.You can include this code at the end of your macro.
Public sub Hidecol()
'Count no. of used columns in 1st row
l = Cells(1, Columns.Count).End(xlToLeft).Column
'Loop through 1st row 1stcolumn till 1st row lastcolumn
For i = 1 To l
'if cell vaue is 0 then hide cell column
If Cells(1, i).Value = 0 Then
Cells(1, i).EntireColumn.Hidden = True
End If
Next i
End Sub
Place this code in ThisWorkbook
module:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Dim rng As Range
Application.ScreenUpdating = False
For i = 2 To 9
Set rng = Cells(1, i)
If rng.EntireColumn.Hidden = 0 Then
If rng.Value = 1 Then rng.EntireColumn.Hidden = 1
Else
If rng.Value = 0 Then rng.EntireColumn.Hidden = 0
End If
Next i
End Sub
It will work from columns B
to I
(columns 2 to 9) on any sheet. Change this number if the columns you need to evaluate changes.
If you want it to only work on a specific sheet, change the first line to Private Sub Worksheet_Calculate()
and place it in that specific sheet's module.
EDIT: Revised code, it should potentially run much faster when there are few changes.
I've tried Vegard's answer, and it works well.
I've also applied this script, which seems to run slightly faster, but overall still pretty slow.
Private Sub Worksheet_Calculate()
For Each cell In Range("B1:BZ1") '**edit if not correct range
Select Case cell.Value <> 0
Case False
cell.EntireColumn.Hidden = False
Case True
cell.EntireColumn.Hidden = True
End Select
Next cell
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.