Pass row to sub with active cell being default

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm attempting to modify a small sub that hides columns based on a key string in the header. I'm getting hung up on two points 1) passing the row number of the header and 2) setting the location of the activecell as the default header location. Here's a failing attempt.

Code:
Sub Hide_Column(strHeader As String, ByRef row_Hdr As Range = ActiveCell.EntireRow.Calculate)

When I use the statement above, the text is red/won't run.
I've attempted to take out the ByRef and place it within the sub ( row_Hdr as Variant); it returns null (clearly not correct).

All suggestions welcome.

Thanks, y'all.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Default values are applicable only for Optional parameters. Also I think that default value can only be a constant.

So you can declare row_hdr as optional and then check if it is null or not in the sub. Probably like this:

Code:
Sub testhide()
    Hide_Column "header"
    Hide_Column "header", Range("1:1")
End Sub


Sub Hide_Column(strHeader As String, Optional row_Hdr As Range)
    If row_Hdr Is Nothing Then row_Hrd = ActiveCell.EntireRow
    row_Hdr.Hidden = True
End Sub
 
Upvote 0
That works perfect. Thanks!!

Appreciate the info re: default values too.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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