Saving a file with a variable pathname and filename

Tarver

Board Regular
Joined
Nov 15, 2012
Messages
109
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
For some unknown (to me!) reason, I can't get this to work. The debugger definitely doesn't like my code, but I don't know why. Anyone have any ideas?

VBA Code:
Sub ReportDaily()

'Set up Variables
    Dim FName   As String
    Dim FPath   As String
    Dim NewBook    As Workbook

    FPath = "X:\Sales\2020 Daily Sales Report\E-Mailed Reports"
    FName = "2020 Daily Sales through " & Format(Now(), "MM-DD") & ".xlsx"

'Do some things

'Save the file
    If Dir(FPath & "\" & FName) <> "" Then
        MsgBox "File " & FPath & "\" & FName & " already exists."
    Else
        MsgBox "File to be saved as " & FPath & "\" & FName
        Workbook.SaveAs FileName:=FPath & "\" & FName
    End If
    
End Sub

I get a Run-time error '424', Object required when the code executes.

Any advice on how to correct this would be most appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Are you trying to save the workbook that contains the code?
 
Upvote 0
In that case it should be
VBA Code:
ThisWorkbook.SaveAs FileName:=FPath & "\" & FName
 
Upvote 0
Well, wait. Correct my hasty reply.

I'm trying to save two workbooks, a new one that has no name (that should be defined by my code) and the original workbook that created the new workbook.
 
Upvote 0
The code you posted is not creating a new workbook.
 
Upvote 0
You're correct. That code is in the 'Do Some Stuff comment section I posted. I left that out because it was long and unnecessary to share, I thought.

The code copies several tabs into a new file. That new file is active when my save code runs. My code runs all the way through to the last line,

VBA Code:
Workbook.SaveAs FileName:=FPath & "\" & FName

where it then errors out.


I appreciate you trying to help me. Sorry that I'm making it confusing.
 
Upvote 0
In that case use ActiveWorkbook, instead of Workbook
 
Upvote 0
Excellent! That works. Thanks very much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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