Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA Hiding Rows

I have a table of data with dates in the first column and then a bunch
of other data across the row. On top of this I have a beginning date
cell and an ending date -- I would like to use VBA to hide the rows
with dates outside of the period (bounded by the begin/end dates). How
do I do this?

Also if it could update as the begin/end dates change -- that would be
ideal.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Hiding Rows


Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:B1")) Is Nothing Then
Cells.Rows.Hidden = False
For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If Cells(i, "A").Value < "" And _
(Cells(i, "A").Value < Range("A1").Value Or _
Cells(i, "A").Value Range("B1").Value) Then
Rows(i).Hidden = True
Else
Rows(i).Hidden = False
End If
Next i
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.




--
HTH

Bob Phillips

wrote in message
ps.com...
I have a table of data with dates in the first column and then a bunch
of other data across the row. On top of this I have a beginning date
cell and an ending date -- I would like to use VBA to hide the rows
with dates outside of the period (bounded by the begin/end dates). How
do I do this?

Also if it could update as the begin/end dates change -- that would be
ideal.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA Hiding Rows

Thanks -- I'm having a hard time with this -- I haven't used Visual
Basic really since I was in high school -- Could you explain what the
code is doing (i.e. how I can adapt it to my spreadsheet)?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Hiding Rows


Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
On Error GoTo ws_exit:


Handle errors, and reset events in this case

Application.EnableEvents = False


This is just to stop this code creating a cascade of changes

If Not Intersect(Target, Me.Range("A1:B1")) Is Nothing Then


This checks whether cells A1 and B1 have changed or not, which is where I
assumed you would store your two dates. Change to suit.

Cells.Rows.Hidden = False


Unhides all rows in case there is one hidden that would not now be hidden
due to a changed date

For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1


Find the bottom row and step back one row at a time

If Cells(i, "A").Value < "" And _
(Cells(i, "A").Value < Range("A1").Value Or _
Cells(i, "A").Value Range("B1").Value) Then


If the date in the current row in column A (change to suit) is less than the
earlier stored date or greater than the later stored date, then ...

Rows(i).Hidden = True


Hide the row

Else
Rows(i).Hidden = False


This is actually redundant.

End If
Next i
End If

ws_exit:
Application.EnableEvents = True


Set events back on again.

End Sub


Note where the code is stored as per my original reoply.

--
HTH

Bob Phillips

wrote in message
ups.com...
Thanks -- I'm having a hard time with this -- I haven't used Visual
Basic really since I was in high school -- Could you explain what the
code is doing (i.e. how I can adapt it to my spreadsheet)?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Color alternate rows when after hiding selected rows Monk[_2_] Excel Worksheet Functions 6 June 7th 08 01:36 AM
Hiding Specific Rows Based on Values in Other Rows Chris Excel Worksheet Functions 1 November 2nd 06 08:21 PM
Hiding a button when hiding rows fergusor Excel Discussion (Misc queries) 2 August 10th 06 02:31 PM
Hiding Rows if the linked rows are blank KG Excel Discussion (Misc queries) 9 May 18th 05 02:32 AM
Copying Rows when hiding other rows Neutron1871 Excel Worksheet Functions 2 November 3rd 04 11:38 PM


All times are GMT +1. The time now is 11:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"