simple code to show a number of rows

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,066
Office Version
  1. 365
Platform
  1. Windows
Rows 11-30 and rows 41-60 both are 20 rows (obviously)

I need a bit of code that when a user enters a number in cell A1, for example 5, the first 5 rows of each of the above 2 sections is visible, the last 15 rows are hidden

Any help would be great :-)

TIA
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi
If I understand
Code:
Sub test()
    r1 = 11: lr1 = 30
    r2 = 41: lr2 = 60
    Cells(1).Offset(r1 - 1 + Cells(1, 1).Value).Resize(lr1 - r1 - 5 + 1).EntireRow.Hidden = True
    Cells(1).Offset(r2 - 1 + Cells(1, 1).Value).Resize(lr2 - r2 - 5 + 1).EntireRow.Hidden = True
End Sub
 
Upvote 0
Here is a change event one to consider.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range, rng2 As Range, numRows As Long

If Not Intersect(Target, Range("A1")) Is Nothing Then
    If IsNumeric(Range("A1")) Then
        numRows = Application.Max(0, Application.Min(20, Range("A1")))
        Set rng = Rows("11:30")
        Set rng2 = Rows("41:60")
        Union(rng, rng2).EntireRow.Hidden = False
        Select Case numRows
            Case 0
                Union(rng, rng2).EntireRow.Hidden = True
            Case 20
            Case Else
                rng.Offset(numRows).Resize(20 - numRows).EntireRow.Hidden = True
                rng2.Offset(numRows).Resize(20 - numRows).EntireRow.Hidden = True
        End Select
    End If
End If

End Sub
 
Upvote 0
Perhaps
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range
If Target.Address(0, 0) = "A1" Then
    On Error Resume Next
    Set r1 = Range("11:30"): Set r2 = Range("41:60")
    r1.EntireRow.Hidden = True
    r2.EntireRow.Hidden = True
    r1.Resize(Target.Value).EntireRow.Hidden = False
    r2.Resize(Target.Value).EntireRow.Hidden = False
End If
End Sub
 
Upvote 0
Thanks all

For the worksheet change codes, where do these need to be placed? I cant get either to do anything so I'm obviously doing something wrong!
 
Upvote 0
For the worksheet change codes, where do these need to be placed?
They need to go into the correct worksheet module.

From excel, right click the sheet tab (i.e. Sheet1), then 'view code'.
 
Upvote 0
Many thanks, all working well now :-)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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