原書 Exercise 2 要求 Sally Suite 寫一個 macro:只顯示 Outcome = Discharged 的 rows,並將結果 rows 填成黃色背景。
來源為 patients.xlsx/Table1;條件為 Outcome=Discharged;格式為 yellow background。
| Requirement | 設定 |
|---|---|
| Table | Table1 |
| Filter field | Outcome |
| Filter value | Discharged |
| Formatting | 符合條件 rows = yellow background |
理想 macro 應先定位 Table1/Outcome column,套 AutoFilter,再只格式化 visible data rows;執行前應確認它不會把 header、hidden unrelated rows 或整張 worksheet 全部填黃。
A 檔正文沒有印出 Exercise 2 的 VBA solution;答案位於外部 11/exercise2_solution.xlsx。依來源規則,本頁不自行冒充官方 macro。
AI-generated VBA 是高風險 automation:效果可能一次改大量 cells,因此比一般 formula 更需要先讀 code、備份 workbook、測試小範圍。
請為 Table1 產生 VBA:篩選 Outcome=Discharged,只將符合條件的 data rows 填黃色;不要改 header 或其他 rows。先逐段解釋程式碼與可能影響範圍,我確認後再執行。
**問題:**如果 macro 用 UsedRange.Interior.Color = ...,為什麼有風險?