Macro not working.

Robert Wyatt

Board Regular
Joined
Jul 15, 2012
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
I have a macro that has stopped working correctly and I'm at my wits end. It says it has either been moved, no longer exits or another program is using it. But I have only one program using this Macro. any help would be greatly appreciated. It does the printing, and posting, but not saving.

Sub SavePayrollKeeper2024WithNewName()
Dim NewFN As Variant
PostToYTD
PostToSocialSecurityRegister
PostToPTORegister


' Copy Next Pay Period to a new workbook
ThisWorkbook.Sheets.Copy
NewFN = "D:\Payroll Keeper 2024\Earning Statements\ Pay Period" & " # " & Range("B6").Value & " - " & Format(Range("B7"), "mm-dd-yyyy") & ".xlsm"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbookMacroEnabled (it's this part that keeps showing up in yellow}
ActiveWorkbook.Close
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Just a guess, since you get that highlighted line it's very likely that this folder doesn't exist:
D:\Payroll Keeper 2024\Earning Statements\
 
Upvote 0
What is the full value of variable NewFN when this error occurs?

It doesn't matter what else is using the macro, it thinks the problem is with the file. Is is possible you already have a file open in Excel with that name? Are you sure the folder exists?

This not causing your problem but declare NewFN as String instead of Variant.
 
Upvote 0
If you want to save the whole workbook (ThisWorkbook.Sheets.Copy), would it not be easier to save the workbook with a new name?

I don't have your workbook but this works here. Changed some naming.
Code:
Dim NewFN As String
NewFN = "C:\Folder Name Here\Temp" & Range("K2").Value & " # " & Range("K3").Value & ".xlsm"
ActiveSheet.Copy    '<---- I used sheet here
ActiveWorkbook.SaveAs NewFn, 52
ActiveWorkbook.Close
End Sub
 
Upvote 0
What is the full value of variable NewFN when this error occurs?

It doesn't matter what else is using the macro, it thinks the problem is with the file. Is is possible you already have a file open in Excel with that name? Are you sure the folder exists?

This not causing your problem but declare NewFN as String instead of Variant.
Yes, the folder does exist and no I do not already have a file open in excel with that name.
 
Upvote 0
If you want to save the whole workbook (ThisWorkbook.Sheets.Copy), would it not be easier to save the workbook with a new name?

I don't have your workbook but this works here. Changed some naming.
Code:
Dim NewFN As String
NewFN = "C:\Folder Name Here\Temp" & Range("K2").Value & " # " & Range("K3").Value & ".xlsm"
ActiveSheet.Copy    '<---- I used sheet here
ActiveWorkbook.SaveAs NewFn, 52
ActiveWorkbook.Close
End Sub
I tried using your code, now it shows Activeworkbook.saveAs NewFn 52 in yellow.
 
Upvote 0
A space before "Pay Period" in the file name seems unlikely is that really correct ?
You can also try putting the line below before your save as:
debug.print NewFN
Then copy the value from the immediate window and try a manual Save As on the file pasting what was in the immediate window in the save box and see what you need to change to make it work.
 
Upvote 0
Stop the macro and just before the highlighted line add this other:
VBA Code:
Debug.Print NewFN
then use Ctrl+G to show the Immediate pane (if not already visible), then relaunch the macro and when the macro stops again post what you read in the Immediate pane.
 
Upvote 0
Re: "Activeworkbook.saveAs NewFn 52 in yellow."
It should as you did not change it to reflect your path.
I doubt very much that this is a valid path on your computer.
Code:
NewFN = "C:\Folder Name Here\Temp" & Range("K2").Value & " # " & Range("K3").Value & ".xlsm"
 
Upvote 0
Stop the macro and just before the highlighted line add this other:
VBA Code:
Debug.Print NewFN
then use Ctrl+G to show the Immediate pane (if not already visible), then relaunch the macro and when the macro stops again post what you read in the Immediate pane.
This is what I ready in the immediate pane.
D:\Time Sheet\Earning Statements\Temp # .xlsm
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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