Hide rows and columns

Macklemore

New Member
Joined
Mar 27, 2013
Messages
3
Hi,

I am very new to VBA. Yes I know it's 2013 but I'm seeing the value in it for the first time. Everything I did prior to this was child's play, not that this is complex to most of you all.

I'm looking to see if there is a way to hide rows and columns via VBA in excel.

Cell b1 contains a company name from a pick list
Cell c1 contains an ever changing column name

Row A2:AM2 contains column names
Row B3:B70002 contains the names of 12 companies

Cell AE2:AM2 contain column names. One of these column names will be displayed in Cell C1

So what I need is;

1. Hide all rows that are not equal to the values in cell B1
2. Hide all columns in the AE:AM range whose AE2:AM2 cells do not equal the value in cell C1
3. All columns AE:AM should hide/unhide if the values in C1.
4. All rows A3:AM70002 should hide/unhide if the values in B1 change.

Any help would be much appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I've searched through the forums and modified a piece of code that work well for hiding rows. I need to now incorporate how to hide columns.

here's my row hiding hack.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ActiveSheet.AutoFilterMode = False
Range("A2:AM7000").AutoFilter
Range("A2:AM7000").AutoFilter Field:=2, Criteria1:=Range("B1").Text

Application.ScreenUpdating = True


End Sub
 
Upvote 0
[Solved] Hide rows and columns

Sorry seems with a little digging I was able to hack more at it. Peter_SSs showed me the way in post http://www.mrexcel.com/forum/excel-questions/687674-show-columns-based-cell-value.html #6

My final code is;

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ActiveSheet.AutoFilterMode = False
Range("A2:AM7000").AutoFilter
Range("A2:AM7000").AutoFilter Field:=2, Criteria1:=Range("B1").Text


Dim cel As Range, Headers As Range
Dim s As String

Set Headers = Range("AE2:AM2")
If Target.Address = "$C$1" Then
s = Target.Value
Application.ScreenUpdating = False
If s = "" Then
Headers.EntireColumn.Hidden = False
Else
For Each cel In Headers
cel.EntireColumn.Hidden = Not cel.Value = s
Next cel
End If
Application.ScreenUpdating = True
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,779
Messages
6,174,488
Members
452,566
Latest member
Bonnie_bb

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