"Offset" in VBA arrays

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,749
Office Version
  1. 365
Platform
  1. Windows
How can I get something like the OFFSET -function to work in a VBA array?

I'm trying to come up with a UDF that counts the days between two weekdays.

My function gets three arguments, starting day as a string and ending day as a string and the third one is minimum number of days between those two.

The array goes from "MON" all the way to the "SUN" three weeks away.

What I'm trying to do is get the UDF to count the days from say "FRI" to "MON" if the minimum days between those two was 4 (= FRI to MON would be 3 but since the minimum days needs to be 4 the function should return 3 + 7 = 10 days).

Solving this on a worksheet would look something like this:
Excel Workbook
BMBNBOBPBQBRBS
2MONTUEWEDTHUFRISATSUN
3*******
4*FromToMin daysAnswer**
5*FRIMON410**
Sheet

How could I solve this in VBA (hopefully without SelectCase arrays).
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi Misca,

What are the dimensions of your Array?
Is it 1x21 or 3x7?

To do the equivalent of Offset in a One-Dimensional Array you just need to add the Index numbers.

If MyArray(idxFrom)="FRI" then
MyArray(idxFrom+10) would evaluate to "MON"

You might simplify your UDF by taking advantage of some of the built-in date functions, but I'm assuming there's a reason you are taking this approach.
 
Upvote 0
Hi Misca,

What are the dimensions of your Array?
Is it 1x21 or 3x7?

To do the equivalent of Offset in a One-Dimensional Array you just need to add the Index numbers.

If MyArray(idxFrom)="FRI" then
MyArray(idxFrom+10) would evaluate to "MON"

You might simplify your UDF by taking advantage of some of the built-in date functions, but I'm assuming there's a reason you are taking this approach.

Is there a way to evaluate all of the values in that offset range(1:10), as opposed to just the 10th?

...if it were an OFFSET formula it, would be the "height" element instead of "rows".
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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