Help with VBA Code

ThiccNugg

New Member
Joined
Oct 24, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,

I need a VBA code that will resave my workbook on my desktop (regardless of user) and switch to xlsx format (macros disabled), and rename the workbook with text and a date pulled from a cell value (BC2). Below is the code I have so far but it does not seem to work (error shown in image below). If someone could please fix this for me that would be great. It would also be nice if it continued to close the current workbook and then opened the newly saved one. Thanks :)

VBA Code:
Sub Resave()

Dim FileName As String
Dim Path As String

Application.DisplayAlerts = False
Path = "C:\users\" & Environ("Username") & "\Desktop"
FileName = "BCA Daily All Arrivals " & ActiveWorkbook.Sheets("MASTER").Range("BC2")

ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Close

End Sub

VBA Error.PNG
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You need to have the backslash after Desktop
Path = "C:\users\" & Environ("Username") & "\Desktop\"
 
Upvote 0
As BC2 is a date you may need to change the format as you cannot use / in a file name
VBA Code:
Path = "C:\users\" & Environ("Username") & "\Desktop\"
FileName = "BCA Daily All Arrivals " & Format(ActiveWorkbook.Sheets("MASTER").Range("BC2").Value, "dd-mm-yyyy")
 
Upvote 0
You need to have the backslash after Desktop
Path = "C:\users\" & Environ("Username") & "\Desktop\"
Yes I did try that but I don't think the path seems to be the problem because I have done debug and hover over path and it is correct.
 
Upvote 0
As BC2 is a date you may need to change the format as you cannot use / in a file name
VBA Code:
Path = "C:\users\" & Environ("Username") & "\Desktop\"
FileName = "BCA Daily All Arrivals " & Format(ActiveWorkbook.Sheets("MASTER").Range("BC2").Value, "dd-mm-yyyy")
Okay I tried that and it still didn't seem to work (same error).
 

Attachments

  • Code Error.PNG
    Code Error.PNG
    13.6 KB · Views: 8
Upvote 0
I solved it. I had the date format in cell BC2 as DD.MM.YYYY instead of DD/MM/YYYY...
 
Upvote 0
Solution
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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