I have an Excel spreadsheet that should be saved on the desktop when the User presses a button. For this I use the following VBA to check if the file already exists on the Desktop:
Sub SaveFileOnDesktop()
Do
New_Filename = Application.InputBox("Please type in name for new file?")
If New_Filename = False Then Exit Sub
If Len(Dir("C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm")) = 1 _
Then MsgBox ("File alreday exists. Please change file name.")
Loop Until Len(Dir("C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm")) = 0 Or New_Filename = False
ActiveWorkbook.SaveCopyAs "C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm"
MsgBox ("File saved successfully on desktop.")
End Sub
Now when the User enters a file name that already exists on the desktop the loop continues. Once the user enters another name the loop does stop. This functionality works fine so far.
The only issue I have is that the MsgBox ("File alreday exists. Please change file name.") does not appear if the user enters an exisitng name into it.
What do I have to change in my code so the message box appears?
This cannot be True, thus the MsgBox does not appear:
If Len(Dir("C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm")) = 1
The length would be at least 10+, if the file is present or 0 if it is not present. 1 cannot be achieved.
To make it appear, write:
If Len(Dir("C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm")) > 1
or even:
If Len(Dir("C:\Users\" & Environ("Username") & "\Desktop\" & New_Filename & ".xlsm"))
Related
I have a macro in excel that if a drive exists the macro saves the file to my harddrive and thumbdrive. If it doesn't exist, it saves to the harddrive. When the macro runs I am getting an error. Here is the macro:
Sub SaveFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim filepath As String
name = "Siemens"
filepath = "F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
If fso.DriveExists("F:\") = True Then
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
'ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:=filepath
Else
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
End If
End Sub
Here is the error I am getting:
I don't know if you can see but the last part of the error message says "\Siemens\8E555720. That should also say the customer name (i.e. Siemens). In the code I have set the customer name in the variable "name". So why is it giving me this crazy error? All help is appreciated.
Something like this might be better:
Sub SaveFile()
Const PATH_C As String = "C:\Users\dgray\Documents\"
Const PATH_F As String = "F:\Dave backup\Open Orders\Label Manifests\" & _
"Active Labels Manifest\Manifest Related\File saving testing folder\"
Dim fileName As String, custName As String
custName = "Siemens"
fileName = custName & " Manifest " & Format(Now, "mm-dd-yyyy") & ".xlsx" 'or .xlsm
ActiveWorkbook.SaveAs fileName:=PATH_C & fileName 'assume C is always available
'save to F if available
If Len(Dir(PATH_F)) > 0 Then
'assumes the custName folder already exists...
ActiveWorkbook.SaveAs fileName:=PATH_F & custName & "\" & fileName
End If
End Sub
I can see the space in folder name which may cause this error.
By removing space in the foldername this error would be fixed.
I'm attempting to have my workbook save as a macro-enabled workbook upon execution of my macro. When the macro is initiated, a userform will populate where the user can select a FiscalYear, FormYear, and a FormMonth. The reason for separate years is because FiscalYear will begin in Oct. Oct will be year 18, however it will begin FY19.
I am attempting to insert the value of the FiscalYear into my SaveAs function. The filepath stops after I use FiscalYear and it places the remaining string from the path in front of the DocName I am wanting the workbook to saveas:
Path "J:\x\y\z\FY" & FiscalYear & "\Templates FY" & FiscalYear
DocName:"G22 Dashboard & " " & FormMonth & " " & "FY" & FiscalYear
The document will save in location "FY & FiscalYear &" as "Templates FY18G22 Dashboard & " " FormMonth & " " & "FY" & FiscalYear
Any advice to show me what I am doing incorrectly in this situation?
Probably a rookie mistake, but any help would be greatly appreciated, thanks!
I have attempted different syntax strategies (use of "" locations, & additions/removals).
Sub Save_Report_As()
'Disabling Display Alerts
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Path = "J:\x\y\z\FY" & FiscalYear & "\Templates FY" & FiscalYear
DocName = "G22 Dashboard" & " " & FormMonth & " " & "FY" & FiscalYear
ActiveWorkbook.SaveAs filename:=Path & DocName, FileFormat:=52
'Enabling Display Alerts
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Will post my comment as an answer so this will stop showing up on the "unanswered" list:
path & "\" & docname & ".xlsx" would be more appropriate... you left out the "\" between the path\docname plus the extension (#RyanWildry caught the extension, too)
I want to build the following:
select sheets for pdf printing - works
create folder and print sheets - works
attach those printed files to an email - doesn't work
the filename depends on cell values + Date (last Range.Value in filename), is there a way to get those pdfs attached?
I tried the following, but that doesn't work
'code ...
Dim myDir as String, mySht as String
myDir = "C:\Users\ihlin\OneDrive\Düngung\" & Worksheets("Drip_Drain_Eingabe").Range("s13").Text
mySht = Worksheets("Druckansicht_mmol").Range("c2").Text & "_" & Worksheets("Druckansicht_mmol").Range("K2").Text & "_" & Worksheets("Druckansicht_mmol").Range("P2").Text & "_" & "mmol_" & Worksheets("Druckansicht_mmol").Range("T1").Text
`code ... ...
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht & ".pdf"
End if
If CheckBox2 = True Then
.Attachments.Add myDir & "\" & mySht2 & ".pdf"
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht3 & ".pdf"
End if
If CheckBox1 = True Then
.Attachments.Add myDir & "\" & mySht4 & ".pdf"
End if
Publishing takes forever and ends with crashing Excel.
Any help would be appreciated.
Here is a simple debugging method. Try the following:
.Attachments.Add "c:\somehardcoded\address\ofthe\worksheet\folder\file.pdf"
If it works, then super, you simply have to find a way to represent the folder.
If it does not work, then forget it and try to attach the file in another way.
I have created an application using excel macro, where the user feeds certain values and saves it to several directory path with a button click macro.
When I select a region from drop down, it should save the file to designated region folder. Say for eg, when NY is selected, the file will be saved to shared drive and 2016 - NY folder. But now, deciding the future of the application, I am thinking of having "year" as a separate field in the worksheet, which retrieves the year value from the user. How do I achieve this without the necessity to change the code every year. The process will be continuing for 'n' number of years from now. Thanks in Advance !
FileName1 = Range("D3").Value
filenameOfNewBook = FileName1
If location = "Illinois" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Illinois\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "LA" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\LA\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "NY" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\NY\" & FileName1 & "-" & "checklist" & ".xlsm"
Else
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Atlanta\" & FileName1 & "-" & "checklist" & ".xlsm"
End If
MsgBox "File Saved successfully!", , "Save"
ActiveWorkbook.save
Application.DisplayAlerts = True
From my experience for office tasks purpose, it's better not to refer to current year, but year set by user, so for example in January 2017 user can still perform actions on files related to 2016. You can get rid of the following:
If location = "Illinois" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Illinois\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "LA" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\LA\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "NY" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\NY\" & FileName1 & "-" & "checklist" & ".xlsm"
Else
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Atlanta\" & FileName1 & "-" & "checklist" & ".xlsm"
End If
And instead use:
Dim myYear as String, locations() as String, locationForPath as String
Dim locCounter as Long
myYear = ThisWorkbook.Worksheets("Sheet1").Range("a2").value2 'the cell with year value, for example 2016
locations = Split("Illinois,LA,NY",",")
For locCounter = LBound(locations) to UBound(locations)
If location = locations(locCounter) Then locationForPath = location: Exit For
Next locCounter
If locationForPath = vbNullString Then locationForPath = "Atlanta"
ActiveWorkbook.SaveAs FileName:="W:\Audits\" & myYear & "\" & locationForPath & "\" & FileName1 & "-" & "checklist" & ".xlsm"
I have the following question. I use a file to log assets (laptops, desktops etc) into certain folders, like deployed, stock, repair and hotswap.
I made some buttons in it which work all fine. One button called deployed, when I save the sheet with this button it saves it with EU IMAC, serial number and date as XLMS file.
I like to change the code from this button, so that when I save a sheet as deployed it automatically delete the XLMS file with serial number and name in the folder stock.
Below the codes for all the save buttons and it's button 61 that needs to be fixed, the others I will change afterwards. The code is form other forum, but with no success.
Sub Button60_Click()
Range("A1:G68").PrintOut
End Sub
Sub Button51_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Hotswap\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Hotswap -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub Button53_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Returned to stock\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Return to stock - " & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub awaitwuhan_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\To repair\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & "- Repair -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub Button61_Click()
p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"
'opslaan
s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
ActiveWorkbook.SaveAs p & s_name
'verwijderen
d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then Kill p & d_name
End Sub
First, give your buttons meaningful names, that is such a garbled mess to try and determine what button60 is or does.
Second You need to use the file system object from Microsoft Scripting Library (add a reference in excel to this dll scrrun.dll) then you can check to see if the file exists and delete it
Sub Button61_Click()
p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"
'opslaan
s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
ActiveWorkbook.SaveAs p & s_name
'verwijderen
d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
'create the file system object
Dim fso As FileSystemObject
Set fso = New FileSystemObject
'make sure the file exists first
If fso.FileExists(p & d_name) = True Then
If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then
fso.DeleteFile p & d_name, True
End If
End If
'free the memory
Set fso = Nothing
End Sub