Delete a worksheet and recreate with the same sheet name - vba

I'm currently trying to delete a worksheet and auto create a new worksheet with the same name.
I'm using the below code. However, when i run the code the windows pop up appears asking me to confirm deletion, i want to prevent this and just delete and replace with a new sheet. I want to avoid Send-keys for this.
ThisWorkbook.Sheets("Process Map").delete
ThisWorkbook.Sheets.Add.Name = "Process Map"

Try setting the DisplayAlerts to False (then back to true if that is what you want as the default setting.
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkBook.Sheets("Process Map").Delete
On Error Goto 0
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map"
Or without error handling:
Application.DisplayAlerts = False
ThisWorkBook.Sheets("Process Map").Delete
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map"

Related

VBA - Application.DisplayAlerts = False not working

This is intended to be a daily macro that run to update a report. This file would need to overwrite the existing file daily. However, the Application.DisplayAlerts = False is not working and I still get the pop up saying that this file already exists and if I want to replace. Is there something wrong with my code or is there a workaround to use a method that would automatically click yes for me?
Sub DailyRefresh ()
'Open and refresh Access
Dim appAccess As Object
Set appAccess = GetObject("S:\Shared\DailyRefresh.accdb")
Application.DisplayAlerts = False
appAccess.Visible = True
appAccess.DoCmd.RunMacro "Run_All_Queries"
appAccess.CloseCurrentDatabase
'Open Excel
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("s:\Shared\Template.xlsx")
xl.Visible = True
Application.DisplayAlerts = False
'Set date to the 1st of the Month on Summary tab
xl.Sheets("Summary").Visible = True
xl.Sheets("Summary").Select
xl.Range("C10").Value = DateSerial(Year(Now), Month(Now), 1)
xl.Range("C10").NumberFormat = "mm/dd/yyyy"
' REFRESH Table
xl.Sheets("Data").Visible = True
xl.Sheets("Data").Select
xl.Range("A1").Select
xl.Range("DailyRefresh.accdb[[#Headers],[ACTIVITY_DT]]").Select
xl.Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
xl.Worksheets("Fname").Visible = True
xl.Sheets("Fname").Select
'Copy and Save AS
Application.DisplayAlerts = False
Path = "S:\Shared\NewTemplate"
Filename = xl.Sheets("Fname").Range("A7").Value
xl.SaveAs Path & Filename & ".xlsx", FileFormat:=51, CreateBackup:=False
xl.Worksheets("Fname").Visible = False
xl.Close
Application.DisplayAlerts = True
End Sub
Application.DisplayAlerts = False
refers to the application where your code is running, not the Excel instance you created.
xl.DisplayAlerts = False
would be what you want.
I propose you to use Application.DisplayAlerts = False exactly before the line(s) that are taking time (no other lines between them). I had the same problem, because I was using it just once, but after each line that calls an external app, when it is back to VBA, it goes back to true.

Auto answer message box

I am writing a short script compiling other macros.
It allows me to run three macros at once if I want to, rather than each one one by one.
I don't want to have the reply to the message boxes in the other 3 macros. The default answer is fine.
I am using the Application.DisplayAlerts = False method. Here:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Run "Mod_Patients.MettreDatesAJourPatients"
Application.Run "Mod_Prescripteurs.MettreDatesAJourPrescripteurs"
Application.Run "Mod_Services.MettreDatesAJourServices"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Data has been updated"
Only that the Application.DisplayAlerts false/true does not work.
The subsequent message boxes appear.
I can't find a workaround.
Here's what Scott is suggesting:
Sub MettreDatesAJourPatients(Optional WarnUser as Boolean = True)
'....
'....
If WarnUser Then Msgbox "Something something..."
'....
End Sub
When called with no arguments WarnUser will be True: if you want to suppress any messages then call with:
MettreDatesAJourPatients False

How to bypass "Macros have been disabled" message and have macros run Workbook_Activate?

I have some VBA code that formats/resizes the screen when the workbook/worksheet is activated.
The code is as follows:
Private Sub Workbook_Activate()
Dim SaveSelection As Object
Set SaveSelection = Selection
Application.ScreenUpdating = False
Range("A1:T50").Select
ActiveWindow.Zoom = True
Application.DisplayFullScreen = False
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
Range("A1:T50").Select
ActiveWindow.Zoom = True
On Error GoTo ExitPoint
SaveSelection.Select
Application.ScreenUpdating = True
ExitPoint:
End Sub
The problem here is that when the user first opens the workbook, they get this Security Warning message "Macros have been disabled", so the macro does not run when the user first opens the workbook because the message appears.
Does anyone know of a workaround to this?
This can often be resolved by including the workbook's folder path in their Trusted Locations.
As sigil pointed out adding the file's folder location to the Trusted Locations will prevent the Enable Content and Enable Macros dialog boxes from appearing.
Alternately, you could create a VBScript file to open the workbook.
Paste this could into NotePad
Adjust the FILE_NAME constant
Hold down Ctrl+S
Click [Save as type]
Select All Files (.)
Save the file with .vbs as it's extension
Const FILE_NAME = "C:\Excel FIles\Hello World.xlsm"
Dim oExcel
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
On Error Goto 0
If TypeName(oExcel) = "Empty" Then Set oExcel = WScript.CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.Workbooks.Open FILE_NAME

Why is this "Delete Method of Range class failed" error popping up?

I am trying to figure out why this "Delete Method of Range Class" error is popping up. It does not always occur when this macro runs, which makes it all the more perplexing.
Can anybody explain this?
Sub ResetSheet()
If WindowsOS Then
'*******************************************************************************************************'
'Resets the Data sheet. Called by the resetSheetButton procedure (located in module: m7_Macros1_5). '
'Also called by the OkCommandButton_Click procedure in the OnOpenUserForm form. '
'*******************************************************************************************************'
Application.EnableEvents = False
Sheet4.Visible = True
Sheet4.Activate
Sheet4.Select
Sheet4.Rows("2:101").Select
Selection.Copy
'TC Edit
Sheet1.Activate
Sheet1.Range("A2").PasteSpecial (xlPasteAll)
'Sheet1.Paste
Sheets("Data").Select
Sheet1.Rows("102:10000").EntireRow.Delete
Sheet4.Visible = False
'TC Edit 2.0 - Adding code to reset the exception checkboxes
If WindowsOS Then
Call resetCheckBoxes
End If
Application.EnableEvents = True
This is the macro code that causes the error (sometimes)
This is the error pop-up
try with below simplified code
Sub ResetSheet()
'If WindowsOS Then
Application.EnableEvents = False
With Worksheets("Sheet4")
.Visible = True
.Rows("2:101").Copy Worksheets("Sheet1").Range("A2")
End With
With Worksheets("Sheet1")
.Rows("102:101").EntireRow.Delete
End With
Worksheets("Sheet4").Visible = False
If windowsOS Then
Call resetCheckBoxes
End If
Application.EnableEvents = True
End Sub

Method or data member not found when executing Application.Screenupdating?

I button calls the following procedure:
Sub ImportData()
Dim currentCalculationMethod As Integer
Application.ScreenUpdating = False
currentCalculationMethod = Application.Calculation
Application.Calculation = xlCalculationManual
Call ClearData
Call LoopFiles
Sheets("Start").Activate
Application.Calculation = currentCalculationMethod
Application.ScreenUpdating = True
End Sub
All of the sudden I am getting an error message that "method or data member not found" and the VBE highlights .ScreenUpdating = False for me. What could be the problem that is causing this?
I am running Excel 2013.
This problem does not seem to exist in other files. I don't know what has happened in this file that is causing this.
EDIT:
When I try to run:
Sub GetFolder()
Dim f As Office.FileDialog
Set f = Application.FileDialog(msoFileDialogFolderPicker)
With f
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show Then
ThisWorkbook.ActiveSheet.Range("folderPath") = .SelectedItems(1) & "\"
End If
End With
End Sub
then I get the same error message and Excel highlights .FileDialog.
I figured out what caused the problem. I had another module that was named application. I guess Excel didn't like this. I can understand that but would have hoped for better guidance by the VBE.