Filtering and exporting schedules

I’m looking for a script that can automatically filter a room schedule (filter by department, for example) and then export that to excel (just the out-of-the-box export), but I have multiple departments and I’m wondering if the script can go from one department to the next and export as it goes (filter by department then export, filter by next department then export) until it runs out of department options. Any ideas out there?

Don’t think of it as filter, export, repeat. Think of it as group by department then export to separate sheets/workbooks. It’s a one-time export. You just need to filter the rooms how you’d like.