How to Add Version Numbers to SharePoint PDF Exports Using VBA in Excel
Question details
The user needs a way to modify an Excel VBA macro to append sequential version numbers to exported PDFs on SharePoint instead of overwriting the existing document.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Exporting an Excel worksheet as a PDF directly to a SharePoint directory using a VBA macro and a date-based naming convention.
- Observed behavior
- The current VBA macro overwrites the existing PDF file because it uses a static or date-based filename without a mechanism to detect or increment versions.
Before modifying your VBA script, ensure you have a backup of your original macro code and proper write permissions for the target SharePoint directory.
Store and Increment Version Numbers in a Worksheet Cell
Use a dedicated cell in your Excel workbook to track the current version number, incrementing it via VBA each time a new PDF is exported.
This method is highly reliable because the version number is saved within the workbook itself. Each time the macro runs, it reads the last used number, generates the new filename, and updates the cell for the next export.
Choose a hidden or dedicated cell in your workbook (e.g., cell A1 on 'Sheet2') to store the current version number. Type '1' in this cell to start the sequence.
In your VBA Editor, declare an integer variable and assign the cell's value to it. For example: Dim fileVersion As Integer and fileVersion = Sheets("Sheet2").Range("A1").Value.
Modify your PDF export filepath string to include the version variable. For example: fileName = "Report_" & Format(Date, "yyyymmdd") & "_v" & fileVersion & ".pdf".
Add a line of code at the end of your macro to increase the cell's value by 1 so it is ready for the next run: Sheets("Sheet2").Range("A1").Value = fileVersion + 1.

Dynamically Check SharePoint for Existing Versions
Use VBA to scan the target SharePoint directory for existing files with the same date, dynamically finding the next available version number.
Create and Run Advanced VBA Macros in WPS Spreadsheet
WPS Office provides robust support for VBA and macros, allowing you to seamlessly automate repetitive tasks like exporting versioned PDFs and managing cell data.
- 1. Open your workbook: Launch WPS Spreadsheet and open the file containing your macro.
- 2. Access the VBA Editor: Go to the 'Developer' tab on the top ribbon and click 'Visual Basic' to open the VBA editor window.
- 3. Edit your code: Locate your PDF export macro in the Project Explorer and modify the filename string to include a version variable.
- 4. Run the macro: Save your code changes, return to your spreadsheet, and click 'Macros' to execute the updated versioning script.

Frequently Asked Questions
Why does my VBA macro overwrite the SharePoint file instead of saving a new one?
If your macro uses a static filename or only includes the current date, running it multiple times on the same day generates the exact same filename. Without a dynamic version number or timestamp, the destination system will overwrite the previous file.
Can I use the current timestamp instead of a version number?
Yes, appending the current time (using Format(Now, "hhmmss")) to the filename instead of a sequential version number ensures every exported file has a unique name, eliminating the need to track a counter in a cell.
How do I format a date in VBA for filenames?
You can use the Format function in VBA. For example, Format(Date, "yyyymmdd") outputs the current date as a continuous string (like '20231025'), which avoids invalid filename characters like slashes.
Does WPS Spreadsheet support VBA macros?
Yes, WPS Office includes comprehensive support for VBA macros. You can enable the Developer tab to write, edit, and run VBA scripts just as you would in Microsoft Excel.




