Skip to content

Instantly share code, notes, and snippets.

@NathanAdhitya
Last active October 6, 2023 12:29
Show Gist options
  • Save NathanAdhitya/9a804ade76964981542616c8b952aac7 to your computer and use it in GitHub Desktop.
Save NathanAdhitya/9a804ade76964981542616c8b952aac7 to your computer and use it in GitHub Desktop.
A simple Excel FOREACH formula function to iterate over an array. Add this code to Name Manager with the name FOREACH.
# Lines starting with # are comments, they are not code.
# Paste this into Name Manager. Give the name "FOREACH"
=LAMBDA(
arr; func; offset;
IF(
offset < COUNTA(arr);
VSTACK(
func(INDEX(arr; offset));
FOREACH(arr; func; offset+1)
);
func(INDEX(arr; offset))
)
)
# Example Usage
=FOREACH({1;2;3;4;5}; LAMBDA(x; x); 1)
=FOREACH({1;2;3;4;5}; LAMBDA(x; VSTACK(x; IF(MOD(x; 2); "Number above me is even."; "Number above me is odd."))); 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment