Newbie Help - VBA to hide rows based on user input

roboto

New Member
Joined
Jan 30, 2012
Messages
4
I am a complete newbie when it comes to writing macros, but I was wondering if someone would be able to help me?

I want to click on a button (I know how to assign a macro to a button) and have a macro run that asks me which row numbers to hide and then pop up a print dialog.

Is this doable? I would appreciate any help any one could give me!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub atest()
Dim x
Dim s As String, i As Long
s = InputBox("Enter row numbers to hide separated by ,")
x = Split(s, ",")
For i = LBound(x) To UBound(x)
    Rows(x(i)).Hidden = True
Next i
End Sub
 

roboto

New Member
Joined
Jan 30, 2012
Messages
4
Thank you for your help! I have one question though, is there a way to make it apply to a range? For example, if I want to hide rows 60-100 is there a way that I don't have to type in each row separated by a comma?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub atest()
Dim x
Dim s As String, i As Long, j As Long, k As Long
s = InputBox("Enter row numbers to hide separated by -")
x = Split(s, "-")
i = x(LBound(x))
j = x(UBound(x))
For k = i To j
    Rows(k).Hidden = True
Next k
End Sub
 

roboto

New Member
Joined
Jan 30, 2012
Messages
4

ADVERTISEMENT

Amazing! Thank you so much. You have made my life so much easier!

I take it there is no way for it to pop up a print dialog automatically then?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub atest()
Dim x
Dim s As String, i As Long, j As Long, k As Long
s = InputBox("Enter row numbers to hide separated by -")
x = Split(s, "-")
i = x(LBound(x))
j = x(UBound(x))
For k = i To j
    Rows(k).Hidden = True
Next k
Application.Dialogs(xlDialogPrint).Show
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,193
Messages
5,768,772
Members
425,492
Latest member
blueexcel123

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
Top