Auto hide columns

xaritona89

New Member
Joined
Mar 17, 2018
Messages
35
Hello,
I have an excel file. I want to automatically hide the corresponding columns during opening.
How can this be done?

mrex.png



P.S. With the help of Python, I automatically open Excel and take a picture. When opening, I want it to be hidden if cell = 0, or visible if cell = 1.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hello,

Try

VBA Code:
Private Sub Workbook_Open()
Dim Cl As Range
For Each Cl In Range("C1:M1")
Cl.EntireColumn.Hidden = Cl.Value = 0
Next Cl
End Sub
 
Upvote 0
Hello,

Try

VBA Code:
Private Sub Workbook_Open()
Dim Cl As Range
For Each Cl In Range("C1:M1")
Cl.EntireColumn.Hidden = Cl.Value = 0
Next Cl
End Sub
i try :

VBA Code:
Private Sub Workbook_Open()


Dim Cl As Range

    For Each Cl In Range("C1:M1")
        Cl.EntireColumn.Hidden = Cl.Value = 0
    Next Cl

End Sub

Then i open it doesn't work.
I saved the file: microsoft excel macro-enabled worksheet.
Code insert in Module1.
 

Attachments

  • test.png
    test.png
    13.1 KB · Views: 6
Upvote 0
Did you put the code in This Workbook under Microsoft Excel Objects ??
 
Upvote 0
The script works for me.
Do not put the script in a module

Install the code like this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Double click on this workbook
Paste the code in the VBA edit window
 
Upvote 0
Let say if 0 and 1 change, the column hide/unhide does not updated, until close the file and reopen.
Therefore, I suggest to use worksheet_activate event.(Right click on sheet name, view code then paste into)
VBA Code:
Option Explicit
Private Sub Worksheet_Activate()
Dim cell As Range, u As Range
Range("C1:M1").EntireColumn.Hidden = False
For Each cell In Range("C1:M1")
    If Not IsEmpty(cell) And cell.Value = 0 Then
        If u Is Nothing Then
            Set u = cell
        Else
            Set u = Union(u, cell)
        End If
    End If
Next
u.EntireColumn.Hidden = True
End Sub
 
Upvote 0
VBA Code:
Private Sub Workbook_Open()


Dim Cl As Range

    For Each Cl In Range("C1:M1")
        Cl.EntireColumn.Hidden = Cl.Value = 0
    Next Cl

End Sub

This code works when I test with a button.
But after saving, when I open it again, the button doesn't work anymore.

Please help me how to save it so that it works when opened.
Where should I put this code?
 

Attachments

  • testtt.png
    testtt.png
    30 KB · Views: 3
Upvote 0
Let say if 0 and 1 change, the column hide/unhide does not updated, until close the file and reopen.
Therefore, I suggest to use worksheet_activate event.(Right click on sheet name, view code then paste into)
VBA Code:
Option Explicit
Private Sub Worksheet_Activate()
Dim cell As Range, u As Range
Range("C1:M1").EntireColumn.Hidden = False
For Each cell In Range("C1:M1")
    If Not IsEmpty(cell) And cell.Value = 0 Then
        If u Is Nothing Then
            Set u = cell
        Else
            Set u = Union(u, cell)
        End If
    End If
Next
u.EntireColumn.Hidden = True
End Sub
my file musb be xlss or xlsm?
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,987
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top