VBA Hide Columns

Status
Not open for further replies.

APML

Board Regular
Joined
Sep 10, 2021
Messages
216
Office Version
  1. 365
Hi all, I'm not so great at VBA so hoping someone can help with the following.
I'm after a simple macro.
When cell A1 = 1, Columns H:L are hidden,
When cell A1 = 2, Column H:L are unhidden

A1 is the result of a formula (i.e 1 or 2 isn't directly entered into A1).
A1 is the result of a formula, i.e A1=K15

I only want this to apply to the worksheet named "Intra"
Cell A1 is on the worksheet named "Intra"
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This will do what you are asking for, but, how is K15 updated ? If itself isn't a formula, once the columns H: L are hidden, how do you access it ?
The event macro needs to be pasted in the "Intra"'s sheet module.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Range("A1")
        Case 1
            Range("H:L").EntireColumn.Hidden = True
        Case 2
            Range("H:L").EntireColumn.Hidden = False
    End Select
End Sub
 
Upvote 0
You could also consider a Worksheet_Calculate() event such as the below, which would be in the Intra sheet's module.

VBA Code:
Private Sub Worksheet_Calculate()

Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
Dim kcell As Range: Set kcell = ws.Range("A1")

If kcell.Value = "1" Then
    ws.Range("H:L").EntireColumn.Hidden = True
ElseIf kcell.Value = "2" Then
    ws.Range("H:L").EntireColumn.Hidden = False
End If

End Sub
 
Upvote 0
Solution
Thx so much, the last code (worksheet calculate) solved the problem. I reeally appreciate your help
 
Upvote 0
the last code (worksheet calculate) solved the problem
Does this mean that my suggestion didn't solve your problem ? Don't you think that I tested it before posting ?!! Maybe you didn't expose everything that was necessary.
 
Upvote 0
Does this mean that my suggestion didn't solve your problem ? Don't you think that I tested it before posting ?!!
I tried it but it didn't work on my sheet but honestly, I really appreciate the help. I'm not very good at VBA so I guess there could've been something else I was doing wrong that prevented it from working
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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