How do I automate the deletion of a worksheet? - vba

I'm using Application.Sheets("Sheet6").Delete to delete a sheet, and it causes a popup that asks if I'm sure. How do I automatically select delete?

Update your code to disable the display of alerts before you delete your sheet; Then enable the alerts after the delete code executes
Application.DisplayAlerts = False
Sheets("Sheet6").Delete
Application.DisplayAlerts = True

You can turn your display alerts off for Excel
Application.DisplayAlerts = False
Application.Sheets("Sheet6").Delete
Application.DisplayAlerts = True

Turn off the alerts before you do the delete. Then turn them back on like this:
Application.DisplayAlerts = False
Application.Sheets("Sheet6").Delete
Application.DisplayAlerts = True

Related

Turning Off ScreenUpdating, EnableEvents and ,DisplayAlerts

I'm using the above code to Open an Excel file from a form. I'm trying to get the file open without the application request to update the Workbook links. But it seems for some reason whenever the file is open the application still popping up the message box asking whether or not to update the links of the workbook. Do you know why the code might not be working properly?
Thank you
Private Sub CommandButton4_Click()
Dim RutaArchivo As String
On Error Resume Next
RutaArchivo = Application.GetOpenFilename(Title:="Test", _
filefilter:="Excel files (*.xlsx), *.xlsx")
Workbooks.Open Filename:=RutaArchivo
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
End Sub
Try to use that to open the returned Workbook:
Set objexcel = CreateObject("Excel.Application")
objexcel.Application.DisplayAlerts = False
Application.Workbooks.Open Filename:=RutaArchivo
objexcel.Application.DisplayAlerts = True

Autosave ALL Excel Files Every X Minutes

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

how to speed up my macro?

I faced an interesting problem with my macro that parses log files (1GB).
Of course there are some settings as follows:
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Application.DisplayAlerts = False
Application.CutCopyMode = False
And also there is one general loop by log file lines with DoEvents within (to prevent excel screen from freezes).
The problem is the macro is very slow when my mouse pointer hovers over cells. Once the pointer moves away from the excel cells, the macro starts working 30 times faster! Any ideas why this happens and how to resolve the problem?
One proposed way is
application.visible=false
but it looks like excel crashes while the macro is running.
Something you can definitely add is:
Application.Cursor = xlWait
Then switch it back at the end
Application.Cursor = xlDefault
This will get rid of the cursor flickering

hiding selected worksheets without for loop

I would like to hide some worksheets, all at once, without using for loop. The reason for this is a big number of worksheets, so my macro works a while.
Do you have any ideas how can I improve the running time of this macro?
You can just record a macro of selecting the first sheet, and then select the last sheet with Shift. Then you right-click and Hide.
Sheets(Array("Sheet2", "Sheet3", "Sheet4")).Visible = False
Or by index
Sheets([{2, 3, 4}]).Visible = False
Note that you can not unhide more than one sheet at a time.
To add a custom view with all sheets visible:
For Each ws In Sheets
ws.Visible = True
Next
ActiveWorkbook.CustomViews.Add "All" ' you can change the name
Then, to un-hide all sheets:
ActiveWorkbook.CustomViews("All").Show
To un-hide the sheets faster, you can try disabling some of the events and calculation:
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
For Each ws In Sheets: ws.Visible = True: Next
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Sub HideSheets()
Dim sSheets() As String
sSheets = Split("Sheet1,Sheet2,Sheet3", ",")
Worksheets(sSheets).Visible = xlHidden
End Sub

Removing Worksheets via Worksheets.Remove()

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