PC Review


Reply
Thread Tools Rate Thread

Can you hide and unhide rows with a macro based on content of colu

 
 
Husker87
Guest
Posts: n/a
 
      22nd Sep 2008
We have a worksheet that collects rows of information from another sheet. Is
there a way to write a macro that would hide (or unhide as new rows are
added) so when I print it only prints rows with data?

Here is our attempt, which hides the rows but will not unhide the rows when
new rows are added. Suggestions???

Private Sub Worksheet_Change(ByVal Target As Range)

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False

With ActiveSheet
For Each cell In Range("B6:B30,B38:B62")
If cell.Value = "" Then
cell.EntireRow.Hidden = True
ElseIf cell.Value > 1 Then
cell.EntireRow.Hidden = False
End If
Next
End With

Application.ScreenUpdating = True
End Sub

 
Reply With Quote
 
 
 
 
aushknotes
Guest
Posts: n/a
 
      22nd Sep 2008
The range of cells you're hiding are hard-coded. Once you change the range of
cells "Range("B6:B30,B38:B62")" to include the new rows, your code should
work.




"Husker87" wrote:

> We have a worksheet that collects rows of information from another sheet. Is
> there a way to write a macro that would hide (or unhide as new rows are
> added) so when I print it only prints rows with data?
>
> Here is our attempt, which hides the rows but will not unhide the rows when
> new rows are added. Suggestions???
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> Application.Calculation = xlCalculationAutomatic
> Application.ScreenUpdating = False
>
> With ActiveSheet
> For Each cell In Range("B6:B30,B38:B62")
> If cell.Value = "" Then
> cell.EntireRow.Hidden = True
> ElseIf cell.Value > 1 Then
> cell.EntireRow.Hidden = False
> End If
> Next
> End With
>
> Application.ScreenUpdating = True
> End Sub
>

 
Reply With Quote
 
Husker87
Guest
Posts: n/a
 
      23rd Sep 2008
Thanks for the reply… when we input values in the rows within the range (say
row 21 or 22) the macro then hides all the blank rows. The problem comes
when we then enter information that populates into row 23 it does not unhide
row 23. Make sense or should I explain with a hard example? Thanks again
for helping…

"aushknotes" wrote:

> The range of cells you're hiding are hard-coded. Once you change the range of
> cells "Range("B6:B30,B38:B62")" to include the new rows, your code should
> work.
>
>
>
>
> "Husker87" wrote:
>
> > We have a worksheet that collects rows of information from another sheet. Is
> > there a way to write a macro that would hide (or unhide as new rows are
> > added) so when I print it only prints rows with data?
> >
> > Here is our attempt, which hides the rows but will not unhide the rows when
> > new rows are added. Suggestions???
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > Application.Calculation = xlCalculationAutomatic
> > Application.ScreenUpdating = False
> >
> > With ActiveSheet
> > For Each cell In Range("B6:B30,B38:B62")
> > If cell.Value = "" Then
> > cell.EntireRow.Hidden = True
> > ElseIf cell.Value > 1 Then
> > cell.EntireRow.Hidden = False
> > End If
> > Next
> > End With
> >
> > Application.ScreenUpdating = True
> > End Sub
> >

 
Reply With Quote
 
aushknotes
Guest
Posts: n/a
 
      23rd Sep 2008
Assuming you always insert new rows at the bottom of the worksheet (I.e.
right after your 2nd range of rows):

Private Sub Worksheet_Change(ByVal Target As Range)

Dim lLastRow As Long

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False

With ActiveSheet
lLastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row

For Each cell In Range("B6:B30,B38:" & lLastRow)
If cell.Value = "" Then
cell.EntireRow.Hidden = True
ElseIf cell.Value > 1 Then
cell.EntireRow.Hidden = False
End If
Next
End With

Application.ScreenUpdating = True
End Sub





"Husker87" wrote:

> Thanks for the reply… when we input values in the rows within the range (say
> row 21 or 22) the macro then hides all the blank rows. The problem comes
> when we then enter information that populates into row 23 it does not unhide
> row 23. Make sense or should I explain with a hard example? Thanks again
> for helping…
>
> "aushknotes" wrote:
>
> > The range of cells you're hiding are hard-coded. Once you change the range of
> > cells "Range("B6:B30,B38:B62")" to include the new rows, your code should
> > work.
> >
> >
> >
> >
> > "Husker87" wrote:
> >
> > > We have a worksheet that collects rows of information from another sheet. Is
> > > there a way to write a macro that would hide (or unhide as new rows are
> > > added) so when I print it only prints rows with data?
> > >
> > > Here is our attempt, which hides the rows but will not unhide the rows when
> > > new rows are added. Suggestions???
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > >
> > > Application.Calculation = xlCalculationAutomatic
> > > Application.ScreenUpdating = False
> > >
> > > With ActiveSheet
> > > For Each cell In Range("B6:B30,B38:B62")
> > > If cell.Value = "" Then
> > > cell.EntireRow.Hidden = True
> > > ElseIf cell.Value > 1 Then
> > > cell.EntireRow.Hidden = False
> > > End If
> > > Next
> > > End With
> > >
> > > Application.ScreenUpdating = True
> > > End Sub
> > >

 
Reply With Quote
 
aushknotes
Guest
Posts: n/a
 
      24th Sep 2008

Too quick when copy & paste. Try the following instead:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim oCell As Range
Dim lLastRow As Long


1 Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False

With ActiveSheet
lLastRow = .Cells(Rows.Count, "B").End(xlUp).Row

For Each oCell In .Range("B6:B30,B38:B" & lLastRow)
If oCell.Value = "" Then
oCell.EntireRow.Hidden = True
ElseIf oCell.Value > 1 Then
oCell.EntireRow.Hidden = False
End If
Next
End With

Application.ScreenUpdating = True
End Sub






"Husker87" wrote:

> We have a worksheet that collects rows of information from another sheet. Is
> there a way to write a macro that would hide (or unhide as new rows are
> added) so when I print it only prints rows with data?
>
> Here is our attempt, which hides the rows but will not unhide the rows when
> new rows are added. Suggestions???
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> Application.Calculation = xlCalculationAutomatic
> Application.ScreenUpdating = False
>
> With ActiveSheet
> For Each cell In Range("B6:B30,B38:B62")
> If cell.Value = "" Then
> cell.EntireRow.Hidden = True
> ElseIf cell.Value > 1 Then
> cell.EntireRow.Hidden = False
> End If
> Next
> End With
>
> Application.ScreenUpdating = True
> End Sub
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to run when I hide or unhide rows in a group IMcC Microsoft Excel Programming 1 27th Mar 2010 12:50 PM
Help to modify a macro for deleting rows based on two or more colu Monomeeth Microsoft Excel Programming 3 20th Jan 2010 07:49 AM
Macro that will unhide then hide rows =?Utf-8?B?bWlua2E=?= Microsoft Excel Misc 10 21st Oct 2006 01:37 PM
How do I hide or unhide zero value columns/rows USING A MACRO? =?Utf-8?B?cGF1bA==?= Microsoft Excel Misc 1 23rd Jan 2006 03:06 PM
Hide/Unhide Rows based on ComboBox cmk18 Microsoft Excel Programming 3 24th Mar 2004 12:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:53 PM.