Data Compilation Time Reduction Using VBA
Most of the time data is gathered from various Sub-divisions, compiled by Divisions, then by Circle offices, Region offices, and finally by Mantralaya.
Google Sheets is a good option, but internet connectivity issues and large file sizes can sometimes create problems.
Since the format is generally kept the same, copy-paste seems like a simple solution — but as we all know, it often consumes a huge amount of time.
Here I have tried to automate this process using VBA.
Demo Video
Here is a short demonstration of how it works:
VBA Script
The following VBA code performs the automation shown in the video. It can be further modified as per your specific requirements.
Features of this script:
- Opens all Excel files in a specified folder
- Reads the required data from each file (currently Sheet 4, rows 1–50, columns 1–20)
- Saves the data in memory (array)
- Closes each file without saving changes
- After processing all files, writes the collected data to the master workbook
Note: Place all source Excel files in a sub-folder named "Files" in the same directory as this macro workbook.
You can adjust the sheet number, row/column ranges, and array sizes according to your actual data structure.
Deep-Dive: Performance & Memory Optimization Mechanics
In high-volume public works and governmental administrative workflows, compiling hundreds of operational workbooks using traditional cell-by-cell loops can introduce severe latency. Understanding the underlying Component Object Model (COM) interface dynamics helps explain why specific optimizations dramatically cut execution time.
1. Cell-by-Cell Access vs. Direct Memory Array Transfer
When executing Sheets(4).Cells(rows, columns).Value inside nested loops, Excel initiates a cross-process COM bridge call for every single cell operation. Reading 50 rows by 20 columns across 100 workbooks results in 100,000 separate COM calls, incurring significant CPU overhead.
By dumping an entire range directly into a 2D Variant array in one step, you reduce thousands of COM calls down to a single read operation per file:
2. Disabling Application Hooks During Batch Processing
To maximize execution speed, suppress background Excel application engines while executing batch aggregation procedures:
| Application Property | Default State | Optimized Execution State | Engine Impact |
|---|---|---|---|
Application.ScreenUpdating |
True |
False |
Eliminates video buffer redraws for every opened file. |
Application.Calculation |
xlCalculationAutomatic |
xlCalculationManual |
Prevents full workbook dependency tree recalculations upon opening files. |
Application.EnableEvents |
True |
False |
Blocks Workbook_Open and Worksheet_Change triggers embedded in sub-division files. |
Application.DisplayAlerts |
True |
False |
Suppresses prompt dialogs (e.g., format mismatches, clipboard queries, macro warnings). |
Enterprise Hardening & Production-Ready Refactored VBA Code
While basic scripts work well in controlled environments, enterprise deployment across diverse workstations requires robust error handling, dynamic range detection, FileSystemObject (FSO) handling, and memory cleanup.
Key Enhancements in Enterprise Architecture:
- Dynamic Data Boundaries: Uses
UsedRangeor dynamic row finding instead of fixed 50-row bounds. - Explicit Workbook References: Replaces
ActiveWorkbookwith qualified object variables to prevent target focus loss. - Error Interception: Uses a structured `Try-Catch-Finally` pattern to ensure screen updating and automatic calculations are restored even if a runtime error occurs.
- Sheet Identification Safety: Resolves sheets by codename or explicit name rather than index (since sheet indexes change if users insert or reorder tabs).
Modern Non-VBA Alternative: Power Query (M Code) Solution
While VBA provides robust programmatic control, Microsoft Power Query (Get & Transform Data) offers a zero-code maintenance alternative built directly into Excel 2016 and later versions. Power Query operates without macro security flags, streams data seamlessly, and automatically handles schema drift.
Why Consider Power Query for Folder Compilation?
- No Macro Security Blocks: Executes safely without requiring user trust settings for
.xlsmfiles. - Self-Healing Schema Handling: Automatically expands, trims, cleans, and converts data types on refresh.
- Automatic Path Parameterization: Directly targets a directory and updates all target tables upon pressing
Ctrl + Alt + F5.
Production Power Query (M Language) Script
To implement this in Excel: Go to Data > Get Data > From Power Query Advanced Editor, and paste the following M Code:
Governance, IT Security & System Deployment Requirements
Enterprise Macro Security Compliance Notice
Deploying VBA solutions across network environments requires strict alignment with organizational cybersecurity frameworks:
- Trusted Locations Configuration: Network shares hosting compilation workbooks must be added to Excel's Trusted Locations via
File > Options > Trust Center > Trust Center Settings > Trusted Locationsto bypass MOTW (Mark of the Web) runtime blocking. - Digital Code Signing: For enterprise distribution, sign the VBA project using a Self-Signed or Certificate Authority (CA) issued
PKI Digital Signature(viaTools > Digital Signatureinside the VBE). - Handling UNC vs Mapped Network Drives: Using relative local paths like
ThisWorkbook.pathcan fail if workbooks are opened via cloud sync engines (e.g., OneDrive / SharePoint HTTP URLs). Use FSO or API wrappers to resolve true UNC paths (\\Server\Share\...) when compiling over network storage.
0 Comments
If you have any doubts, suggestions , corrections etc. let me know