How to Automatically Edit a CSV or Excel File Using VBA
Question details
The user needs to automatically update an unwanted heading in a CSV or Excel report that is generated and placed into a folder every six hours, without opening the file manually.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating repetitive formatting tasks on incoming scheduled reports generated by a third-party tool.
- Observed behavior
- The automated report contains an unwanted heading like 'Food Eaten (percentage)' that needs to be replaced with 'Food Eaten' automatically upon file arrival.
Ensure that macros are enabled in your spreadsheet software settings and that you have the exact file path where the automated report is saved.
Write a VBA Macro to Open, Edit, and Save the File
Create a VBA script that opens the target workbook, modifies the specific cell containing the unwanted heading, and saves the file automatically.
This method allows you to run a script that performs the edit in the background. Once the macro is created, you can trigger it using a scheduled task or folder monitoring tool to achieve full automation without manual intervention.
Launch your spreadsheet application and press Alt + F11 to open the Visual Basic for Applications (VBA) Editor.
In the top menu, click 'Insert' and select 'Module' to create a blank workspace for your script.
Paste the following code, modifying the file path and cell reference to match your needs: Sub UpdateTitleInfo() Workbooks.Open Filename:="C:\Users\Username\Desktop\test.xlsx" Sheets("Name").Cells(1, 5) = "Food Eaten" ActiveWorkbook.Close SaveChanges:=True End Sub
To run this without manual clicks, set up a Windows Scheduled Task that opens this specific macro-enabled workbook shortly after the report is scheduled to arrive.

Automate Folder Monitoring with Power Automate
Use Power Automate to detect when a new file arrives in the folder and instantly trigger a script to clean the data.
Automate Spreadsheet Tasks Seamlessly with WPS Office
WPS Spreadsheet offers powerful macro capabilities, allowing you to run VBA scripts to automate repetitive tasks like editing incoming CSV or Excel files with ease.
- 1. Download WPS Office: Download and install the free version of WPS Office from the official website.
- 2. Access Macro Tools: Open WPS Spreadsheet, navigate to the 'Tools' tab, and click on 'Macros' to access the VBA environment.
- 3. Insert Your Script: Open the VBA Editor, insert a new module, and paste your file-editing script.
- 4. Run the Automation: Execute the script to seamlessly process and update your incoming reports.

Frequently Asked Questions
Can I run a VBA macro without opening the spreadsheet application at all?
VBA requires the spreadsheet application (like Excel or WPS Spreadsheet) to process the commands. However, you can run it invisibly in the background by setting the application visibility to false (Application.Visible = False) using a VBScript trigger.
Does this VBA code work for CSV files as well as XLSX files?
Yes, the Workbooks.Open method in VBA can open CSV files. Once opened, you can manipulate the cells and save it, but be sure to specify the correct FileFormat parameter if you are explicitly saving it back as a pure CSV.
How do I prevent the macro from failing if the file hasn't arrived yet?
You can add error handling in your VBA code using 'On Error Resume Next' or check if the file exists using the 'Dir' function before attempting to execute 'Workbooks.Open'.
How can I automatically trigger the macro exactly when a file is added to a local folder?
While Excel VBA cannot constantly monitor a folder natively without freezing the application, you can use Windows Task Scheduler to run a script periodically, or use third-party tools like Power Automate Desktop to trigger your macro immediately upon file detection.




