Fix Excel Macro Triggers Antivirus Warning Running CMD Commands
Question details
The user's Excel VBA macro is blocked by Microsoft Defender or triggers an antivirus warning when attempting to run CMD.EXE processes.

- Product
- Microsoft Excel
- Device & OS
- Windows
- Scenario
- Running a VBA macro that repeatedly executes command-line scripts, accesses local files, and utilizes hidden command windows.
- Observed behavior
- Antivirus software flags the macro as suspicious behavior and blocks the process via AMSI unless real-time protection is disabled.
Ensure you have full administrative rights to your computer and verify that the external scripts or commands your macro is calling are completely safe before modifying any security settings.
Trust the Workbook and Digitally Sign the Macro
Marking the Excel file as trusted and digitally signing your VBA code helps the antivirus recognize the macro as safe rather than malicious.
Open Excel, go to File > Options > Trust Center > Trust Center Settings. Navigate to Trusted Locations, click 'Add new location', and select the folder where your workbook and external scripts are stored.
Navigate to your Microsoft Office installation folder in Windows File Explorer and run the 'SelfCert.exe' tool to create a local digital certificate for your projects.
Open the VBA Editor in your workbook (Alt + F11). Go to Tools > Digital Signature, click 'Choose', and select the digital certificate you just created to sign the macro project.

Modify VBA Code to Avoid Hidden CMD Processes
Repeatedly launching hidden CMD windows is a common malware tactic. Changing how your macro interacts with the system can prevent AMSI triggers.
Try WPS Office for a Secure and Lightweight Experience
If Microsoft Office's strict AMSI policies continuously block your safe macros and disrupt your workflow, consider using WPS Office. WPS Office provides excellent compatibility with Microsoft Excel files and offers a secure, resource-friendly environment for managing your daily spreadsheet tasks.
- 1. Download WPS Office: Visit the official WPS website to download and install WPS Office Free on your Windows PC.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing .xlsm or .xlsx files directly without any formatting loss.
- 3. Enable Macros Safely: Navigate to the Developer tab to manage your VBA projects within a secure and optimized environment.

Frequently Asked Questions
Why does my macro trigger an antivirus warning even though it only reads local files?
Antivirus software uses behavioral heuristics to monitor actions. Executing hidden processes (like CMD.EXE) directly from an Office document is a common technique used by malware. Consequently, the antivirus may block the action regardless of your benign intent.
Can I simply disable Windows Defender to run my macro?
While disabling real-time protection temporarily allows the macro to run, it is highly discouraged. Doing so leaves your system vulnerable to real malware. It is much safer to digitally sign the macro or move the file to an Office Trusted Location.
Does changing the window state from vbHide to vbNormalFocus solve the antivirus block?
In some cases, making the command window visible reduces the suspicion score of the process, allowing it to run. However, modern AMSI (Antimalware Scan Interface) policies may still block the execution based on the specific commands being run inside the shell.
How do I change the Macro Runtime Scan Scope in Windows?
Modifying the Macro Runtime Scan Scope requires editing Windows Group Policies (typically under Office 2016 > Microsoft > Windows > MacroRuntimeScanScope). Because this impacts system-wide security, it should generally be performed by an IT administrator.




