VBA & Macro Problems

How to Add Version Numbers to SharePoint PDF Exports Using VBA in Excel

Muhammad TalhaMuhammad Talha Oct 12, 2026 868 views

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.

How to Add Version Numbers to SharePoint PDF Exports Using VBA in Excel
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 you start

Before modifying your VBA script, ensure you have a backup of your original macro code and proper write permissions for the target SharePoint directory.

Solution 1Recommended

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.

1
Identify a tracking cell

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.

2
Read the value in VBA

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.

3
Append the version to the filename

Modify your PDF export filepath string to include the version variable. For example: fileName = "Report_" & Format(Date, "yyyymmdd") & "_v" & fileVersion & ".pdf".

4
Increment the counter

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.

Store and Increment Version Numbers in a Worksheet Cell
Advanced VBA Support: For complex VBA programming issues involving SharePoint integration, consider posting your code snippet on Stack Overflow using the 'VBA' tag for community guidance.

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. 1. Open your workbook: Launch WPS Spreadsheet and open the file containing your macro.
  2. 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. 3. Edit your code: Locate your PDF export macro in the Project Explorer and modify the filename string to include a version variable.
  4. 4. Run the macro: Save your code changes, return to your spreadsheet, and click 'Macros' to execute the updated versioning script.
Fully compatible with Microsoft Excel VBA scripts and macrosBuilt-in PDF converter for seamless document exporting via VBALightweight application that runs smoothly even on older devicesFamiliar spreadsheet interface reduces the learning curve
microsoft office alternative - wps office

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.