I don't think so you need a macro. I believe, you are using calculated dimension to limit the KEY field values in the script.
1. Get the all the possible values for KEY and pass into variable
2.. Apply the filter to remove the unwanted data from variable [calculated dimension filter]
3. Select the KEY field
Please post sample qvw ...