VBA - Hide Rows / Columns

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,539
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Using below code to hide rows based on column A if the value does not equal 1

Code:
Sub hide()

  Range("A1", Range("A" & Rows.Count).End(xlUp)).AutoFilter 1, "1", , , 0
  
End Sub

Is there a way I can use this code to hide some columns ?

Any help would be appreciated

Regards,

Humayun
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Maybe this way...for Row 1

Code:
Sub MM1()
Dim c As Long
    For c = 1 To Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
        If Cells(1, c) <> 1 Then Columns(c).EntireColumn.Hidden = True
    Next c
End Sub
 
Upvote 0
Thanks for the reply Michael,

Not working friend-

Run-tiem error 91
Object variable or with block variable not set


Further I would want to hide rows and columns both


For example
Rows From 1 to 10 with values in Columns A which does not equal 1
Columns From A to G with values in Row 14 which dies not equal 1
 
Upvote 0
Not exactly the same method, as your code uses autofilter. But this code should check row to and hide columns where the value is not 1:
Code:
Sub ColHide()
For n = 1 to Columns.Count
If Cells(2, n).Value = 1 Then
Cells(2, n).EntireColumn.Hidden = False
Else
Cells(2, n).EntireColumn.Hidden =True
End If
Next
End Sub
 
Upvote 0
I submitted my post before seeing your previous reply.
Are you saying that you want to hide columns where no rows contain the value 1, and hide rows where no columns contain the value 1?
 
Last edited:
Upvote 0
Hi Trevor,

Thanks for the reply

As mentioned in Post # 3

Hide Rows From 1 to 10 If values in Columns A does not equal 1
Hide Columns From A to G If values in Row 14 does not equal 1


Also if we can set the range in the code for Rows 1 to 10 & for Columns A to G
In this way the code will look into the defined range instead of looking into the entire sheet - I reckon
 
Upvote 0
How about
Code:
Sub hrayani()
Dim Cl As Range
Range("A1:A10").AutoFilter 1, "1", , , 0
For Each Cl In Range("A14:G14")
   Cl.EntireColumn.Hidden = Cl.Value <> 1
Next Cl
End Sub
 
Upvote 0
Or try this to do rows and columns:
Code:
Sub HideMacro()
 
Dim myRowRange As Range
Dim myColRange As Range
Set myRowRange = Range("A1:A10")
Set myColRange = Range("A14:G14")
 
For Each cell In myRowRange
If cell.Value = 1 Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next
 
For Each cell In myColRange
If cell.Value = 1 Then
cell.EntireColumn.Hidden = False
Else
cell.EntireColumn.Hidden = True
End If
Next
 
End Sub

The two Set... rows near the top define the ranges that are checked for 1's.
 
Upvote 0
Hi,

Thanks to everybody for the solution. All working fine.

But I am gonna go with Fluff"s code
Amended as per my actual ranges & using it in the worksheet change event

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim Cl As Range
Range("A1:A142").AutoFilter 1, "TRUE", , , 0
For Each Cl In Range("E143:AL143")
   Cl.EntireColumn.Hidden = Cl.Value <> True
Next Cl


End Sub


This one is the fastest & shortest :) Thanks Fluff
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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