Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default hide empty rows on open

Hi,

I have a spreadsheet that has cell references to another spreadsheet.
I am trying to write a macro script that when the document is opened
(and refreshes data from the linked spreadsheet) it looks at the rows
which are blank and hides these rows.

So far I have found the following code below which works great,
however this macro re-runs everytime you navigate back to the
worksheet, causing the screen to flicker for a few seconds.

Private Sub Worksheet_Activate()
Dim rng As Range, cell As Range
Set rng = Application.Intersect(ActiveSheet.UsedRange, Range
("B6:G120"))
For Each cell In rng
If Application.CountA(cell.EntireRow) = 0 Then cell.EntireRow.Hidden =
True
Next
Application.ScreenUpdating = True
End Sub


I just want the script to run once.

I have tried replacing the top line of script with:


Private Sub Worksheet_Change(ByVal Target As Range)


However I was then getting a debug error associated with line 3.

Can anybody offer any help?

Thanks in advance!
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default hide empty rows on open

You turned screenupdating back on at the end of the procedure, but you never
turned it off at the top of the procedure. If you had, it flickering would have
been hidden.

One the chance that you wanted to not do the hiding of rows except for the first
time...

Option Explicit
Dim HasBeenRun As Boolean
Private Sub Worksheet_Activate()
Dim rng As Range
Dim cell As Range

If HasBeenRun = True Then
Exit Sub
End If

Set rng = Nothing
On Error Resume Next
Set rng = Application.Intersect(Me.UsedRange, Me.Range("B6:B120"))
On Error GoTo 0

If rng Is Nothing Then
'no cells in the used range
'do nothing
Else
Application.ScreenUpdating = False
For Each cell In rng.Cells
If Application.CountA(cell.EntireRow) = 0 Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.ScreenUpdating = True
End If

HasBeenRun = True

End Sub


ps. I only looked at the cells in column B. But since you're using the
entirerow, there's no need to loop through columns C, D, ..., G.


Joshy wrote:

Hi,

I have a spreadsheet that has cell references to another spreadsheet.
I am trying to write a macro script that when the document is opened
(and refreshes data from the linked spreadsheet) it looks at the rows
which are blank and hides these rows.

So far I have found the following code below which works great,
however this macro re-runs everytime you navigate back to the
worksheet, causing the screen to flicker for a few seconds.

Private Sub Worksheet_Activate()
Dim rng As Range, cell As Range
Set rng = Application.Intersect(ActiveSheet.UsedRange, Range
("B6:G120"))
For Each cell In rng
If Application.CountA(cell.EntireRow) = 0 Then cell.EntireRow.Hidden =
True
Next
Application.ScreenUpdating = True
End Sub

I just want the script to run once.

I have tried replacing the top line of script with:

Private Sub Worksheet_Change(ByVal Target As Range)

However I was then getting a debug error associated with line 3.

Can anybody offer any help?

Thanks in advance!


--

Dave Peterson
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
How can i auto hide empty rows in printing in excel? TM Excel Discussion (Misc queries) 3 August 1st 08 07:42 PM
Is there a way to automatically hide empty rows? Christine Excel Worksheet Functions 1 April 12th 07 10:22 PM
Hide Empty Rows When Printing Bob Excel Worksheet Functions 5 August 20th 05 12:57 PM
Format to hide empty rows tamato43 Excel Discussion (Misc queries) 4 May 10th 05 10:16 PM
Macro to hide rows with empty cells tp58tp Excel Worksheet Functions 2 November 13th 04 02:01 PM


All times are GMT +1. The time now is 05:58 AM.

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"