I need to open and close a file using a macro, but I don't want to save it. I can get to wear excel prompts you to Save or Don't save, what is the VBA command for dont save. This is what I am using I just need it to not save and close excel all the way.
Sheets("Sheet1").Select
Range("A1").Select
Sheets("Sheet6").Select
Range("A1").Select
Workbooks.Open Filename:= _
"X:\File.xlsx"
Workbooks.Close
Place False in first argument after Close method to tell VBA to not save the Workbook changes. So:
Workbooks.Close False
or
Workbooks.Close SaveChanges:=False
If I understand well, try to use Application.DisplayAlerts:
Application.DisplayAlerts=False
Workbooks.Close
Application.DisplayAlerts=True
You can use the Saved property of the Workbook object for this. Setting this property to True will stop the prompt from appearing (but won't actually save the workbook):
Dim wb as workbook
Set wb = Workbooks.Open("X:\File.xlsx")
' do stuff here
wb.Saved = True
wb.Close
See http://msdn.microsoft.com/en-us/library/ff196613.aspx for reference
Related
I'm trying to save a file with a new password, but when I reopen the file still asks me for the old password. The code I have is as follows:
FileCopy strLastMonthFilePath, strFilePath
' Open MF
Workbooks.Open Filename:=strFilePath, Password:=OldPassword, UpdateLinks:=0
MF_FileName = ActiveWorkbook.Name
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Password:=NewPassword
Application.DisplayAlerts = True
' Close Source files
Application.DisplayAlerts = False
Windows(MF_FileName).Activate
Workbooks(MF_FileName).Close
' This line does not work. If if change it to the OldPassword then it works
Workbooks.Open Filename:=strFilePath, Password:=NewPassword, UpdateLinks:=0
I tried also to assign the workbook and change the password to a variable but didn't work:
Set myWBk = Workbooks.Open(Filename:=strFilePath, Password:=OldPassword , UpdateLinks:=0)
myWBk.SaveAs Password:=NewPassword
Any idea why the password does not change?
(1) ThisWorkbook is always the workbook where the code is stored. Opening a workbook makes it the ActiveWorkbook. The ActiveWorkbook is the workbook that has currently the focus. However, it is much better to store the workbook reference into a variable
(2) SaveAs creates a copy of the workbook. You need to use Save (or Close with parameter True). However, Save doesn't have a parameter to set or change the password, but this can easily be done using the Password property of the workbook.
Try this:
Dim myWBk As Workbook
Set myWBk = Workbooks.Open(filename:=strFilePath, Password:=OldPassword, UpdateLinks:=0)
myWBk.Password = NewPassword
myWBk.Close True
I looked this topic up and found some help but the suggestions do not seem to be working.
I am opening a CSV file into EXCEL make some changes and then want to save the results back to the same file name and the CSV format.
I want to do this without the prompt that I am getting to make sure I want to save the file.
We are using a macro enabled excel file to import the data make changes and then save.
This whole process with initiated by a batch file that will open the Excel application and the designated file at regular period of time so that is why we do not want the prompt to stop the process.
Here is the code I am using in VBA to do the work, as well as the other subs I found that were suppose to help me suppress the prompt.
This code is in the TheWorkbook of the file and not a module.
Am I missing something?
code
Sub fixfile()
Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
Dim wbkS As Workbook
Dim wshS As Worksheet
Dim wshT As Worksheet
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
Set wbkS = Workbooks.Open(Filename:=strFileName)
Set wshS = wbkS.Worksheets(1)
wshS.UsedRange.Copy Destination:=wshT.Range("A1")
wbkS.Close SaveChanges:=False
'This is the area of work that we doing to the data
'Through here
Application.DisplayAlerts = False 'IT WORKS TO DISABLE ALERT PROMPT
ActiveWorkbook.SaveAs Filename:= _
"W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
:=xlCSVMSDOS, CreateBackup:=False
Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
Application.Quit
End Sub
Private Sub Workbook_Open()
fixfile
End Sub
Sub CloseandSave()
ActiveWorkbook.Close SaveChanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
The problems in your code are due to the following.
When you call SaveAs on the macro-enabled workbook, you had already appended a worksheet to it:
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
and then you're trying to save it as csv, which is a text file with only one worksheet, so Excel complains that you will loose information.
Moreover, you're doing the update to the csv twice: once in the
ActiveWorkbook.SaveAs Filename:= ...
Where, as a result, the current workbook becomes the saved workbook, and then again in the Workbook_BeforeClose. In the latter you dont disable the alerts, but anyway there's no need to save again.
I have come to the conclusion that what you want is to use the macro-enabled wb just as a utility for calculation, and you care only for updating the CSV workbook.
For simplicity, we will disable the alerts for the whole session, because the macro-enabled WB is used as a utility and we dont want the batch job to stop for any reason. However you can do it the traditional way, before and after saving, if you feel more comfortable with it.
' Code module ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
fixCSVFile
' Do the following only if you want the macro-enabled WB to keep
' a copy of the CSV worksheet. but my feeling is you dont want to
' ThisWorkbook.Save
'''''''''''''''''''''
Application.Quit
End Sub
Sub fixCSVFile()
Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
Dim wbkS As Workbook, wshS As Worksheet, wshT As Worksheet
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
Set wbkS = Workbooks.Open(Filename:=strFileName)
Set wshS = wbkS.Worksheets(1)
wshS.UsedRange.Copy Destination:=wshT.Range("A1")
wbkS.Close SaveChanges:=False
'This is the area of work that we doing to the data
' For purpose of testing:
wshT.Range("A1").Value = wshT.Range("A1").Value + 1
' Now we will export back the modified csv
wshT.Move '<- Here we have a temporary workbook copy of the modified csv
With ActiveWorkbook
.SaveAs Filename:=strFileName, FileFormat:=xlCSVMSDOS, CreateBackup:=False
.Close False
End With
End Sub
One more thing, the macro-enabled WB is now such that it closes as soon as it opens so it will be difficult to edit or modify (although there are workarounds). Therefore you should save a back-up copy of it without the Application.Quit, as a testing/maintenance copy. Only the copy that you put in production for the sake of the batch job should have the Application.Quit statement.
Based on the comment in the answers that the reason for opening the file and immediately saving it with no other changes...
So we needed to do what we were doing to get the file edit date to
change but not the actual file.
...this is a complete X-Y problem. If you need to change a file's modified time, just change the file's modified time instead of jumping through all of the opening and re-saving hoops:
Sub UpdateFileModifiedDate()
Const filePath = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
Dim handle As Integer
handle = FreeFile
Open filePath For Binary As #handle
'Read the first byte.
Dim first As Byte
Get #handle, 1, first
'Write it back
Put #handle, 1, first
Close #handle
End Sub
This will be insanely faster than your current process, will only set the file modified date and time to the time that you run the Sub, and doesn't risk any of the other issues you can run into cycling a CSV file through Excel (date formats and locale issues, truncating decimals, conversions to exponential notation, etc., etc.).
since you're going to consciously overwrite an existing file you can just:
first delete it with a Kill command
then do the SaveAs
so change this code section:
'This is the area of work that we doing to the data
'Through here
Application.DisplayAlerts = False 'IT WORKS TO DISABLE ALERT PROMPT
ActiveWorkbook.SaveAs Filename:= _
"W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
:=xlCSVMSDOS, CreateBackup:=False
Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
Application.Quit
to this:
'This is the area of work that we doing to the data
'Through here
Kill strFileName '<-- delete the old file
ActiveWorkbook.SaveAs Filename:= _
"W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
:=xlCSVMSDOS, CreateBackup:=False
Application.Quit
furthermore your code can be refactored by properly handling the ActiveWorkbook and ActiveSheet objects and reduce the variables and code amount, like follows:
Sub fixfile()
Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
Workbooks.Open(Filename:=strFileName).Worksheets(1).UsedRange.Copy Destination:=Worksheets.Add(After:=Worksheets(Worksheets.Count)).Range("A1") '<--| open 'strFileName', reference and copy its 1st worksheet 'UsedRange' and paste it to a newly added worksheet in the macro workbook. After this statement we're left with the opened workbook as `ActiveWorkbook`
ActiveWorkbook.Close SaveChanges:=False '<--| close `ActiveWorkbook`, i.e. the just opened one. We're left with macro workbook as `ActiveWorkbook` and its newly created worksheet as `ActiveSheet`
'This is the area of work that we doing to the data
'Through here
ActiveSheet.Move '<--| move `ActiveSheet` (i.e. the newly created sheet in macro workbook) to a "new" workbook having that sheet as its only one. We're left with this "new" workbook as `ActiveWorkbook`
Kill strFileName '<--| delete the "old" 'strFileName'
ActiveWorkbook.SaveAs Filename:=strFileName, FileFormat:=xlCSVMSDOS, CreateBackup:=False '<--| save `ActiveWorkbook` (i.e the "new" one) as the new 'strFileName' file
ActiveWorkbook.Close SaveChanges:=False '<--| close `ActiveWorkbook` (i.e the "new" one) without changes (we just "SavedA"s it)
Application.Quit
End Sub
It seems like you are making changes to two files. In addition to the csv file that you are opening, you appear to be adding a sheet to the excel file that is running the VBA code with these lines:
Dim wshT As Worksheet
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
So my guess is that you are indeed suppressing the save prompt for the csv file but you are also getting a save prompt for the changes you made to the excel workbook when you attempt to close it. So I think you need to suppress that prompt as well by also turning off DisplayAlerts in the CloseAndSave sub, or wherever the excel workbook is actually being closed.
I don't get why you are copying the CSV sheet into a new sheet in the macro enabled workbook. This is where your problem starts!
You should just be dealing with the data in wshS and saving wbkS instead.
Done, no more problems.
When you call
ActiveWorkbook.SaveAs Filename:= _
"W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv",
FileFormat:=xlCSVMSDOS, CreateBackup:=False`
you're renaming the current macro enabled file within excel to the CSV file as far as Excel sees it.
When Application.Quit is called, it is going to call
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
Which is where the prompt that you are complaining about is happening.
Even if you remove it, after Workbook_BeforeClose is called, Excel is still going to check all the open files' .Saved flag.
Excel will prompt you to save any files where .Saved = False
But if you set ThisWorkbook.Saved = True then Excel will close the file without asking to save.
Solution:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
I have a Macro that opens a workbook (wb1) and copies it into another workbook (wb2), then closes wb1. However I am always prompted the message below that there is a large amount of clipboard data which I don't want to be prompted. After doing some research I found putting the 'Application.CutCopyMode' set to false (which clears the clipboard) would resolve this issue but it hasn't.
Application.CutCopyMode = False
...
'copy the range from source book
wb1.Worksheets("Sheet1").Range("A1:V2").Copy
'paste the data on the target book
wb2.Worksheets("Sheet1").Range("A1:V2").PasteSpecial
wb.Close savechanges:=False
How can I close the file without this message?
If you want to suppress any messages, add the following before closing the file:
Application.DisplayAlerts = False
If you want to clear the clipboard content, you should add the Application.CutCopyMode = False after performing the Copy/Paste operations. Like this:
...
'copy the range from source book
wb1.Worksheets("Sheet1").Range("A1:V2").Copy
'paste the data on the target book
wb2.Worksheets("Sheet1").Range("A1:V2").PasteSpecial
Application.CutCopyMode = False
wb.Close savechanges:=False
What exactly is the special pasting you want to do? If you just want to copy the values across, then instead of copying & pasting, just assign the values directly:
wb2.Worksheets("Sheet1").Range("A1:V2").value = wb1.Worksheets("Sheet1").Range("A1:V2").value
So I've written a script that opens a certain workbook using its directory pathway (through text from a userform textbox) and I want to be able to close it at the end of the script.
My script currently opens a workbook using the file directory and copies something from that workbook and pastes it into the current workbook. The only thing I want to add is that workbook closing at the end of the sub.
Sub A()
Dim wbk As Workbook
strFirstFile = Userform1.Command.Text
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Sheet1")
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
End With
Set wbk = ThisWorkbook
wbk.Sheets("dog").Range("A1").Insert
End Sub
Bear with me I'm a super newbie.
To close the Workbook:
wbk.Close
If you want to save the workbook beforehand, do:
wbk.Save
wbk.Close
I have the following code, it is about to reopen the current excel file.
Sub CloseMe()
Application.OnTime Now + TimeValue("00:00:02"), "OpenMe"
ThisWorkbook.Close False
End Sub
Sub OpenMe()
MsgBox "The file is reopened"
End Sub
I am trying to make it applicable to activeworkbook, so i change
ThisWorkbook.Close False
to
ActiveWorkbook.Close False
but it ended up close the activeworkbook but didnt reopen the file, any advise? Very sorry if this question seem silly to you.
Try this:
Sub ReOpen()
Application.DisplayAlerts = False
Workbooks.Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
Application.DisplayAlerts = True
End Sub
I think there's a thought error ... when the workbook is closing, the contained VBA code is closing as well, so there's no code left to be executed 2 seconds later, and no object that would be the subject of any code.
This will only work if your closing/reopening logic is outside the sheet you want to close/reopen, and to be more specific, residing in a workbook that remains open all the time between closing/reopening the sheet you want to reopen.
I like this, which I adapted from "How To Close And Reopen Active Workbook?"
Sub ReOpen()
ActiveWorkbook.ChangeFileAccess xlReadOnly, , False
Application.Wait Now + TimeValue("00:00:01")
ActiveWorkbook.ChangeFileAccess xlReadWrite, , True
End Sub
It seems more elegant to me and warns if there are unsaved changes. While the original uses ThisWorkbook, I use ActiveWorkbook like #Taosique.