Solved

How to hide a block of rows in middle of spreadsheet with excel vba

Posted on 2011-09-11
4
202 Views
Last Modified: 2012-05-12
I have a spreadsheet that displays Physician specialties.  There are 160 some physician specialties so I need to give the user the ability to only see the top 50 specialties.  I've done this with the following code:
Private Sub cmdtop50_Click()
    Dim wks As Worksheet
    Dim lastrow As Long

    Set wks = ActiveSheet

    lastrow = Range("A" & Rows.Count).End(xlDown).row
    wks.Rows("61:" & lastrow).Hidden = True
   
End Sub

THe spreadsheet has a graphical design for the first 150 rows.  Then there is raw data and the graphical design portion consists of v-lookups to the raw data (I did not design this).  So when I hide rows after row 60 I just go ahead and hide everything -- all the way to the bottom of the spreadsheet.  That works ok but it is pretty bad code I have to admit.  Now I need to create a click event that allows the user to SEE all 150 specialties.  I'd like to let the user see the 150 specialties without seeing the raw data.  So what I'd like is to unhide rows 61 through 150, but keep hidden the rows from 150 to the last row of the spreadsheet. Is there a way to do this without actually entering row numbers?

Thanks,
0
Comment
Question by:dkcoop03
  • 2
4 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 36520323
You do need to specify row numbers, unless there's a gap between the specialities and raw data that you could build a range on.

The unhidE command would be:

wks.rows("61:150").Hidden = false

Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 250 total points
ID: 36520895
A couple other comments.  Your cmdTop50_click() routine could use a couple tweaks.

Try this:

Private Sub cmdtop50_click
dim wks as worksheet
dim lastRow as long

  set wks = activesheet
  wks.Rows("61:" & wks.Rows.Count).Hidden = True
end Sub

to unhide (per my prior post) - use this:

Private sub cmdShowSpecialities_Click()
dim wks as worksheet

  set wks = activesheet
  wks.Range("61:150").Hidden = False
end Sub

Cheers,

Dave
0
 
LVL 6

Expert Comment

by:TinTombStone
ID: 36521229
Why not just use Excel Outlining function?

Select the rows, then goto: Data Tab, Outline Group, Group

Or in 2003

Select the rows, then goto: Data Menu, Group and Outline, Group

It's worth playing with and no code to worry about
0
 

Author Closing Comment

by:dkcoop03
ID: 36523242
Thanks again Dave.  I have to do all of this in code based on events so the outlining function was not an option.  This workbook was created by my boss so I have to work with his material.   I was a little sticky on the show all piece but this will work great.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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