?
Solved

Auto Hide using Excel Macro

Posted on 2011-04-19
3
Medium Priority
?
935 Views
Last Modified: 2012-08-13
Hi,

I'm trying to get excel to hide/unhide rows based on cell values

Sheet 1 Coloumn A has a list of values say 1 to 5
Sheet 2 references these values in Coloumn A: =Sheet1!A1

When a change is made to Sheet 1 it is reflected in sheet 2 and if the cell now has a value = 0 it hides the row. That bit works OK however if I revese the change to sheet 1 the row is not un-hidden on sheet2

Any help gratefuly recieved

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Last As Long
Dim Rng, c As Range
Last = Cells(Rows.Count, "C").End(xlUp).Row
Set Rng = Range("C1:C30" & Last)
Application.ScreenUpdating = False
For Each c In Rng
If c.Value = "0" Then
c.EntireRow.Hidden = True Or c.EntireRow.Hidden = False
End If
Next c
Application.ScreenUpdating = True
End Sub


0
Comment
Question by:BarryAllen
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Arno Koster
ID: 35424685
instead of hiding the row, you could also use the formula

=if(sheet1!A1 <> ""; Sheet1!A1; "")

this works but does not hide the complete row.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35424688
I suggest you use the Change event in Sheet1 to show/hide in sheet2 since formulas do not trigger the Change event:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Last As Long
Dim Rng, c As Range
If Not Intersect(Target, range("C:C")) Is Nothing then
Application.ScreenUpdating = False
For Each c In Intersect(Target, range("C:C"))
   c.EntireRow.Hidden = (c.Value = 0)
   Sheets("Sheet2").Rows(c.Row).Hidden = (c.Value = 0)
Next c
Application.ScreenUpdating = True
End If
End Sub

Open in new window


in Sheet1 for example.
0
 
LVL 19

Accepted Solution

by:
Arno Koster earned 1000 total points
ID: 35424738
looking at the supplied code, it uses the C column and not the A column to check for lines hidden or not.

please note that when the editing takes place in cell C1001, excel processes the range C1 up to C301001 !

but the biggest problem is in :

If c.Value = "0" Then
c.EntireRow.Hidden = True Or c.EntireRow.Hidden = False
End If

this should be

If c.Value = "0" Then
  c.EntireRow.Hidden = True
ELSE
  c.EntireRow.Hidden = False
End If

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question