Save an Excel Workbook to OneDrive with a VBA Filename
Question details
Create a VBA macro that saves a macro-enabled workbook to each user's OneDrive SAFER folder using the current date and values from a specified worksheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants a filename built from the date and worksheet data, even when that worksheet is not active.
- Observed behavior
- The workbook should be saved as an .xlsm file in the user's OneDrive SAFER folder with the intended worksheet values in its filename.
Confirm that OneDrive is synced locally and that a SAFER folder exists in the intended OneDrive account. Identify the worksheet name and cells containing the filename values; the example below uses a Windows OneDrive path.
Save to the OneDrive SAFER folder with an Excel VBA macro
Use an explicit worksheet reference and the user's local OneDrive path to build and save the filename.
The example reads cells B2 and C2 from a worksheet named Data, takes up to 12 characters from each value, and adds the current date in yymmdd format. Change the sheet name and cell references to match your workbook.
This Windows example uses the OneDrive environment variable. If it points to the wrong account or is unavailable, replace the folder assignment with the correct local OneDrive path.
In Excel, open the workbook that contains the macro and press Alt+F11. Select Insert > Module.
Paste this code into the module: Sub SaveToOneDriveSAFER() Dim ws As Worksheet Dim folder As String Dim fileName As String Set ws = ThisWorkbook.Worksheets("Data") folder = Environ$("OneDrive") If folder = "" Then MsgBox "OneDrive path not found. Set the local path in the macro." Exit Sub End If folder = folder & "\SAFER" If Dir(folder, vbDirectory) = "" Then MsgBox "SAFER folder not found: " & folder Exit Sub End If fileName = Format(Date, "yymmdd") & "_" & _ FilePart(CStr(ws.Range("B2").Value2)) & "_" & _ FilePart(CStr(ws.Range("C2").Value2)) & ".xlsm" ThisWorkbook.SaveAs Filename:=folder & "\" & fileName, _ FileFormat:=xlOpenXMLWorkbookMacroEnabled End Sub Private Function FilePart(ByVal value As String) As String Dim ch As Variant value = Left$(Trim$(value), 12) For Each ch In Array("<", ">", ":", Chr$(34), "/", Chr$(92), "|", "?", "*") value = Replace(value, ch, "_") Next ch FilePart = value End Function
Replace Data, B2, and C2 with your worksheet name and cells. In Excel, press Alt+F8, select SaveToOneDriveSAFER, and click Run.
Open the local OneDrive SAFER folder in File Explorer and check the .xlsm filename. Allow OneDrive to finish syncing before accessing the file from another device.
Work with Excel-format files in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook tasks. VBA macro support can vary, so verify this Excel macro in your WPS environment before relying on it.

Frequently Asked Questions
How does the macro read a worksheet that is not active?
It uses ThisWorkbook.Worksheets("Data") and reads cells through that worksheet reference. Replace Data with the exact name of the worksheet containing your values.
What if my OneDrive folder is not found?
Check that OneDrive is set up and synced locally. If Environ$("OneDrive") does not identify the intended account, replace the folder assignment in the VBA module with that account's local OneDrive path.
Why does the saved file use the .xlsm extension?
The macro uses the .xlsm extension and xlOpenXMLWorkbookMacroEnabled format so the saved workbook can retain VBA code.
What happens if a filename contains invalid characters?
The FilePart function replaces common Windows filename characters such as slashes, colons, and question marks with underscores. Review the source cell values if the resulting filename is not what you expect.




