I know there is a way to save one file every x minutes by defining it in the file's modules or Worksheets. Here is a code I found:
Sub SaveThis()
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:05:00"), "SaveThis"
End Sub
Question: Is it possible to have this run on any active file? I was trying to use Personal Macro Workbook, but I cannot figure out how to have it automatically run on the open file.
Thanks in advance.
Just loop through the workbooks collection:
Application.DisplayAlerts = False
For w = 1 to Workbooks.Count
Workbooks(w).Save
Next w
Application.DisplayAlerts = True
Related
Please advise on the following:
I have a code that should refresh formulas in my workbook and then copy the results as values to the other sheet. The problem is that the code flies right through everything and copies data even before the refresh is completed (refresh takes approx. 10-15 seconds).
I have used the below (and its varieties) but it didn't do anything to slow it down:
Application.Wait (Now + TimeValue("0:00:15"))
Do I have to do a loop that would check the cell content and execute "copy" part once it identifies the cell is updated?
Sub Excel_VBA_Timer_Event1()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim V As Workbook: Dim x, y, z As Worksheet
Set V = ThisWorkbook: Set x = V.Sheets(1): Set y = V.Sheets(2): Set z = V.Sheets(3)
z.Columns("A:O").EntireColumn.Delete
Aplication.Run "RefreshAllWorkbooks"
'WAIT HERE for approx 15 seonds
'At the moment the code flies through till the end before Bloomberg formulas are refreshed
y.Columns("C:M").Copy
z.Range("A1").PasteSpecial (xlPasteValues): z.Range("A1").PasteSpecial (xlPasteFormats)
z.Activate: z.Cells(1, 1).Select
Application.CutCopyMode = False
MsgBox "Done"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I have some macros that copy my sheet in excel,and delete certain data. Unfortunately the buttons to which the macros are assigned do not copy over when the macros are run.
Sub CandD()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sh As Shape, strtSh As Worksheet
Set strtSh = ActiveSheet
Sheets("BM Condition").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "BM Condition" & Sheets.Count - 1
Range("E14:E33,I14:I33,M14:M33").ClearContents
For Each sh In ActiveSheet.Shapes
sh.Delete
Next sh
strtSh.Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
This is the macro I am using. I have very limited VBA experience and am not finding google very helpful. Could someone recommend a fix for my buttons not copying over?
EDIT: I forgot to mention that when manually copying the buttons remain. I am not sure why this is.
As FunThomas mentioned, I've tried and tested the following without any errors:
Sub CanD()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sh As Shape, strtSh As Worksheet
Set strtSh = ActiveSheet
Sheets("BM Condition").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "BM Condition" & Sheets.Count - 1
Range("E14:E33,I14:I33,M14:M33").ClearContents
strtSh.Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I am attempting to remove Worksheets via VBA code. The general idea here is that when the number of worksheets reaches 18, we delete the end worksheet. Now as worksheets are a collection, I have attempted:
If Worksheets.Count = 18 Then
Worksheets.Remove (Worksheets.Count)
End If
But get an error. Any ideas?
Thanks!
You need to disable alerts first, and then use the delete method.
Application.DisplayAlerts = False
Worksheets(Worksheets.Count).Delete
Application.DisplayAlerts = True
Try using Worksheets.Delete("SheetName") instead: https://msdn.microsoft.com/en-us/library/office/ff837404.aspx.
If Worksheets.Count = 18 Then
Worksheets.Delete(Worksheets.Count)
End If
Try this:
Sub WorksheetDelete()
Application.DisplayAlerts = False 'To avoid display alert
Do Until ActiveWorkbook.Worksheets.Count>=18
ActiveWorkbook.Worksheets(18).Delete 'Delete the 18th worksheet
Loop
Application.DisplayAlerts = True 'Turn on the Alerts
End Sub
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.
I've an Excel macro that runs on opening the file, opens another excel file, refreshes the data, saves and then closes it.
I also have a middle bit (Dim AckTime) that displays a pop up message for second to show what it's done.
BUT.. Since I set the macro to run on opening the workbook using Public Sub Workbook_Open() the message box pops up but will not close automatically on 1 second anymore.
Can anyone help?
Public Sub Workbook_Open()
Application.ScreenUpdating = False Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
With Workbooks.Open("\\filename.xlsm")
ActiveWorkbook.RefreshAll 'updates the data
ActiveWorkbook.Sheets("Dashboard").Range("A2").Value = DateTime.Now
' updates this cell with the current time
Dim AckTime As Integer, InfoBoxWebSearches As Object
Set InfoBoxWebSearches = CreateObject("WScript.Shell")
AckTime = 1
Select Case InfoBoxWebSearches.Popup("Updated, saving & closing...", _
Case 1, -1
End Select
.Save
.Saved = True 'when saved..
.Close 0 'close the file
End With
End Sub
Select Case InfoBoxWebSearches.Popup("Updated, saving & closing...", AckTime)
Should be your only error. You just didn't set the wait time.