How to use sub or function array result in NETWORKDAYS formula

Skiff

Board Regular
Joined
May 30, 2016
Messages
58
Office Version
  1. 2019
Platform
  1. Windows
Hello!
Firstly - maybe answer is already somewhere here, but search give only different problems answers.

I've made this neat macro that calculates days free of work and then populates this into new sheet, makes named range from it to use it in NETWORKDAYS formula last parameter.
But I'm always wondering could I not do this by adding another sheet to workbook? I have similar function that tests one day, but for this formula you need all work-free days in a year.

Already tried with similar sub that returns array, but I cannot use array in Excel formula?
So maybe I can add something like named range for array to use in in formula? Or any other way?

Best Regards
Skiff
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Something like this should do it.
VBA Code:
Sub test()
Dim v As Variant, v2 As String, n As Long
v = Array(1, 2, 3)
    v2 = "={"
        For n = LBound(v) To UBound(v)
            v2 = v2 & v(n) & ","
        Next
    v2 = Left(v2, Len(v2) - 1) & "}"
        ActiveWorkbook.Names("rng").RefersToR1C1 = v2
End Sub
 
Upvote 0
Thanks! But that's not enough sadly.
Had to change v2 = "={" to v2 = "{" because of an error that said wrong formula

dni_wolne is original subroutine result
dni_wolne2 is new
1587732502472.png


I'm testing further and try to get those double quotes

EDIT:
Had to make some changes, but it worked. Thanks!
VBA Code:
Sub test()
Dim v As Variant, v2 As String, n As Long
v = Array(1, 2, 3)
v2 = "={"
For n = LBound(v) To UBound(v)
v2 = v2 & """" & v(n) & """" & ","
Next
v2 = Left(v2, Len(v2) - 1) & "}"
ActiveWorkbook.Names("rng").RefersToR1C1 = v2
End Sub

I also had to change delimiter since in polish lanugage "," is delimiter for decimal point and in formulas ";" is used. But that's only for languages that uses "," as decimal point delimiter.

Result:
1587733154847.png
 
Last edited:
Upvote 0
Solution
Dates and regional formats can often be problematic if not dealt with correctly, glad to see you figured it out while I was offline. I should have tested it with actual dates rather than just numbers for simplicity.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
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