VBA File Save As and pull file name from Cell

tmpollard

Board Regular
Joined
Jun 12, 2008
Messages
111
Hello All,
I'm trying to get VBA code that will do a file save as and the name of the file will be pulled from a specific cell on the spreadsheet, however the extension will be changed from .xlsm to .xlsx so that I don't mess up my template file.
This file is on a network location.
Thank you.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Something like this?

VBA Code:
Sub save_to_xlsx()
    Dim folder As String, filename As String, fullpath As String
    
    folder = "S:\NetworkDrive\Folder\" ' Change to the network location
    filename = Range("A1").Value2 ' Change "A1" to the cell with the file name
    fullpath = folder & filename & ".xlsx"
    Debug.Print fullpath ' If you want this in immediate window
    
    ' Use this code below to stop the pop-up that asks whether you want to save
    Application.DisplayAlerts = False

    ' Finally save the copy 
    ThisWorkbook.SaveCopyAs fullpath

    End Sub
 
Upvote 0
This is how I edited the code based on your recommendations, I don't get any errors but I don't see the filed saved in this location.
Thanks


Sub save_to_xlsx()
Dim folder As String, filename As String, fullpath As String

folder = "\\ucapsn01\share\Plant Information\Raw Data\Shift Details" ' Change to the network location
filename = Range("A10").Value2 ' Change "A1" to the cell with the file name
fullpath = folder & filename & ".xlsx"
Debug.Print fullpath ' If you want this in immediate window

' Use this code below to stop the pop-up that asks whether you want to save
Application.DisplayAlerts = False

' Finally save the copy
ThisWorkbook.SaveCopyAs fullpath


End Sub
 
Upvote 0
I'm not familiar with your setup, but I know I always need a drive letter in front of the folder.
 
Upvote 0
Oh, and you also need to end the folder with the backslash. I should have put an If statement to add that trailing backslash at the end if it wasn't there already.
 
Upvote 0
If you can figure out the drive letter, replace [DRIVE_LETTER] below with it, and I think you'll have better luck.

VBA Code:
Sub save_to_xlsx()
    Dim folder As String, filename As String, fullpath As String
    
    ' Change below to the network location
    folder = "[DRIVE_LETTER]:\ucapsn01\share\Plant Information\Raw Data\Shift Details"
    filename = Range("A10").Value2 ' Change "A10" to the cell with the file name
    
    If Right(folder, 1) <> "\" Then
        fullpath = folder & "\" & filename & ".xlsx"
    Else
        fullpath = folder & filename & ".xlsx"
        End If
        
    Debug.Print fullpath
    
    ' Use this code below to stop the pop-up that asks whether you want to save
    Application.DisplayAlerts = True
    
    ThisWorkbook.SaveCopyAs fullpath

    End Sub
 
Upvote 1
Solution
Glad to hear it worked, and you're welcome!

Is there an option to mark the post as answered? It helps me out when looking through my answered questions.
 
Upvote 1

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top