Hide rows of a named range based on user input

ClintMc

New Member
Joined
Dec 4, 2017
Messages
4
Hey Guys & Gals -- Creating a template for Program Management and trying to add some VBA. New to VBA and what I thought would be straightforward is of course giving me a challenge.


  • taskRange = all the potential rows in the chart, would like to hide based on user input (currently 1 column with 100 rows in it and values consisting of 1-100)
  • numOfTasks = user input cell telling the program how many tasks they would like to have for this project
  • Getting a compile error on the IF/THEN statement saying "Expression Expected"
  • The rest should be evident from the code I am hoping

Code:
Sub UserSelectsNumOfTasks()
'
' This code will run when user changes number of tasks on the worksheet
'
  Dim WorkRng As Range
  Dim Rng As Range
  Dim numTasks As Integer


'  MsgBox "You changed the number of tasks"
  
'  setting the active range as the range of all the tasks
  Set WorkRng = Range("taskRange")
  numTasks = Range("numOfTasks").Value
  
'  MsgBox WorkRng.Address
'  MsgBox numTasks
  
  ' this for loop should go through all the rows in the range and hide
  ' the rows whose value is larger than user selected value
  For Each Rng In WorkRng
    If Rng.Value > numTasks Then
        Rng.EntireRow.Hidden = True
    End If
  Next Rng


End Sub

I am sure it is a newbie thing I am missing but any help is much appreciated!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
No clue what happened but when I retyped it as below it started working. Any insight would be great!

Code:
Sub UserSelectsNumOfTasks()
'
' This code will run when user changes number of tasks on the worksheet
'
  Dim WorkRng As Range
  Dim Rng As Range
  Dim numTasks As Integer


'  MsgBox "You changed the number of tasks"
  
'  setting the active range as the range of all the tasks
  Set WorkRng = Range("taskRange")
  numTasks = Range("numOfTasks").Value
  
  MsgBox WorkRng.Address
  MsgBox numTasks
  
  ' this for loop should go through all the rows in the range and hide
  ' the rows whose value is larger than user selected value
For Each Rng In WorkRng
    If Rng.Value > numTasks Then
    Rng.EntireRow.Hidden = True
    End If
Next Rng


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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