Using VBA to hide rows

kleinmat

New Member
Joined
Mar 12, 2024
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I have a macro-enabled Excel Sheet that is basically a work planner and fundamentally looks like this (well, in reality with thousands of rows and over 100 columns):

mark2.jpg


What I now would like to achieve is to create a separate Tab that contains only those rows that say Team 1, Team 2 (the grey rows) and those that have a Task Description on them.
Like so:

mark5.jpg


Not sure if it has an impact, but:
1. The blue markings on the original tab are done through conditional formatting
2. The second Tab (the one with the schedule and the hidden rows) is fully protected (read only for the user)
3. That second Tab will also have rows and columns hidden by default (the usable space is over 10.000 rows long and over 100 columns wide, but anything below that last line ~10.000 and every column to the right of the ~100s column will be hidden anyways)

I tried several code fragments that I could find here in this forum, but none worked for me.
I should say that I am newish to Excel, so I apologize if this is trivial and I just don't know the solution.

Thank you so much!
Matt
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
try this.

VBA Code:
Sub Hide()


Worksheets("Sheet2").Activate
ActiveSheet.Unprotect Password:="yourpassword"

lr = Cells(Rows.Count, "B").End(xlUp).Row

Rows("5:" & lr).Hidden = True 'first lets hide all the rows

For i = 4 To lr 'Cells(Rows.Count, "B").End(xlUp).Row
    If Cells(i, "A").Value <> "" Or Cells(i, "B").Value <> "" Then Rows(i).Hidden = False
    If Cells(i, "A").Value <> "" Then Rows(i - 1).Hidden = False
Next i

ActiveSheet.Protect Password:="yourpassword"

End Sub

hth,
Ross
 
Upvote 1
Solution
Thank you! But how/when does that trigger?
I have only added that code (slightly modified to the actual sheet I use) into the VBA worksheet of that Schedule Tab (Sheet10), but it seems to never be executed.
Sorry, that is a newbie question - but that is what I am.
Thank you!
Matt

hide.jpg
 
Upvote 0
insert a shape, picture, icon, text box etc... anywhere on you worksheet.
Right click on the object you just made - selected assign macro. then click on the macro that you created.

now anytime you click on the object the macro will run.
 
Upvote 0
Thank you. Does that even work within a fully protected sheet? I want Sheet10 to be fully read-only.
 
Upvote 0
Regardless, I added a Shape and assigned this function. But when I press it, I get a "Subscript out of range" error message. Any idea how to debug that?
Thank you very much for your help!
 
Upvote 0
you don't have a worksheet named "Sheet10". you renamed it to "Schedule"

change Worksheets("Sheet2").Activate to Worksheets("Schedule").Activate
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,813
Members
452,996
Latest member
nelsonsix66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top