How to Pass ranges in functions so correct cell in range is passed?

Damckinleysr

New Member
Joined
Jan 6, 2015
Messages
3
I have a row with the Year and Month in each cell in the format 201401 (YYYYMM).

201401 is in A1 The progression is 201401 201402 201403 ..... 201512.

If I named the Range "YYYYMM" (=Sheet1!$A$1:$X$1) Excel will automatically put in the proper value- if I put =YYYYMM in B1 it will display 201401. If I fill right for 23 cells each cell will have =YYYYMM and Excel will put in the proper value for that column. For example: K2 will display as 201411.

I have a function that counts the number of Friday's in the month
Fuction (YYYYMM as String) as Integer

if put = GetFridaysinMonth (K1) in K2 it clearly passes 201411 as the string and returns 4.

To make the worksheet more readable I want to use the name range YYYYMM as an argument. If I put = GetFridaysinMonth (YYYYMM) in K2 how can I make the function know the value of YYYYMM is 201411?



Fuction GetFridaysinMonth (YYYYMM as range) as Integer


How do I figure out it is in col K and value is 201411? If have tried byVal, etc and cannot figure out right combination. Any clues?


End Function

Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You have a function, its argument is a string.

1) From your description, it sounds like what is in the cell is an Excel Serial Date formatted to YYYYMM format. That format changes what one sees in the cell, but not the underlying value.

2) One nice thing about UDF's is the automatic conversion that is provided by Excel.

If you have a function

MyFunction (someString as String) As String

And you want to use it on the string in cell A1, you don't have to pass the range as an argument, you put the cell reference in the worksheet formula
=MyFunction(A1) and excel does the rest.

As to your function, look into what is actually in the cell. is it a number or a a string or a date with an unusual format. One quick way to tell is to format one of those cells as General and look at it (including whether its aligned left or right).
 
Upvote 0
Thanks - I pass the argument the way you recommend MyFunction(A1) already. The problem is the spreadsheet has dozens of functions with multiple variables and is not very readable.

Instead of Naming the Range YYYYMM I could have named it Year_Month.

The actual function is looking more like this -=Number_Events(D$1,D$9,D$3,$B$20) as all the arguments are in Ranges - I think something like =Number_Events(Year_Month, Barn , Poss_Days, Day_Type) is much more readable. I cannot figure out how to pass a Range Name like this and have the function figure out what cell in the range it should be.

Thanks for the help




Rental Rates Per Event
Weekdays
Fridays

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
this is similar to what I am trying to do
Name Col A Lenth
Name Col B Width
Col C is the formula = Width * Length
Excel will calculate all the cells in Col C correctly - Even though formula is = Width * Length in all cells
Col A Col B Col C
Length Width
Area
414
326
51995
414
414
414
414

<colgroup><col style="width:48pt" span="3" width="64"> </colgroup><tbody>
</tbody>

Instead of using the formual = Width * Length in each cell I want to do = AreaCalculation (Width, Length) and have it work in a similar way.
 
Upvote 0

Forum statistics

Threads
1,215,615
Messages
6,125,854
Members
449,266
Latest member
davinroach

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