Turning Off ScreenUpdating, EnableEvents and ,DisplayAlerts - vba

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

Related

Error in Copying a Sheet from One Workbook to an Another

Encountered below error in my vba code.
Run-time error '1004':
Copy Method of Worksheet Class failed
Public Sub ExportAsCSV(savePath)
Set ws = Workbooks("OND Estimator").Worksheets("port") 'Sheet to export as CSV
Set wb = Application.Workbooks.Add
ws.Copy Before:=wb.Worksheets(wb.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
wb.SaveAs Filename:=savePath, FileFormat:=xlCSV
Application.DisplayAlerts = True
wb.Close savechanges:=False
End Sub
Just copy the worksheet to no location. This creates a new active workbook with a single worksheet (a copy of the original) ready to be saved as xlCSV.
Public Sub ExportAsCSV(savePath)
Workbooks("OND Estimator").Worksheets("port").Copy 'Sheet to export as CSV
Application.DisplayAlerts = False 'Possibly overwrite without asking
with activeworkbook
.SaveAs Filename:=savePath, FileFormat:=xlCSV
.Close savechanges:=False
end with
Application.DisplayAlerts = True
End Sub
try
Public Sub ExportAsCSV(savePath)
Application.DisplayAlerts = False 'Possibly overwrite without asking
With Workbooks("OND Estimator").Worksheets("port").Parent
.SaveAs Filename:=savePath, FileFormat:=xlCSV
.Close savechanges:=False
End With
Application.DisplayAlerts = True
End Sub
I found out the issue on this! I didn't realize hiding the sheets will cause the copy method to not work correctly...
Worksheets("port").Visible = xlVeryHidden
...so what I did is that I revealed the sheet before copying and then hid again once the copy is done.
Worksheets("port").Visible = True
Thank you guys for your helping me!

Overflow error while opening 2nd Excel

I am looking for help after many hours of research.
My problem is a little bit tricky but...
I am working on new Excel-VBA Applications for my company.
I developped some code to help me to open new instances of Excel everytime I open a new workbook.
My problem comes when i try to open a second Excel while my workbook is already open. I get an un-understandable error 6 Overflow.
There are the tools that I developped to open new Excel instances.
Option Explicit
Private WithEvents ThisExcelApplication As Application
Private WithEvents OtherExcelApplication As Application
Private l_blnSaveNewWorkbook As Boolean
'Procédure : Workbook_Open
'Event : opening workbook
Public Sub Workbook_Open()
Debug.Print Time & " : ThisWorkbook\Workbook_Open()"
If Application.Workbooks.Count > 1 Then
Call ReOpenApplicationInNewExcelInstance
Else
l_blnSaveNewWorkbook = False
End If
Set ThisExcelApplication = Application
Load ufConnection
ufConnection.Show
End Sub
There is my Workbook_open, it checks if there is an existing workbooks opened then it opens my workbook on a new excel instance. But my problem doesn't come from here.
Private Sub ThisExcelApplication_WorkbookOpen(ByVal Wb As Workbook)
Dim strFile As String
If Wb.FullName = ThisWorkbook.FullName Then Exit Sub
Set OtherExcelApplication = CreateObject("Excel.Application")
strFichier = Wb.FullName
Wb.Close False
If g_blnApplicationVisibility Then
ThisExcelApplication.Visible = True
Else
ThisExcelApplication.Visible = False
End If
OtherExcelApplication.Visible = True
OtherExcelApplication.Workbooks.Open strFile
End Sub
Private Sub ReOpenApplicationInNewExcelInstance()
Dim oExcel As Excel.Application
Dim strFiler As String
Dim strTemporaryFileName As String
l_blnSaveNewWorkbook = True
Set oExcel = CreateObject("Excel.Application")
strFile = ThisWorkbook.FullName
strTemporaryFileName = ThisWorkbook.Path & Application.PathSeparator
& "TemporaryFileName.xlsb"
'save current workbook on a temporary file to free the access of original workbook
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.SaveAs strTemporaryFileName
Application.EnableEvents = True
Application.DisplayAlerts = True
'close the original workbook and re-open it on new instance
oExcel.Workbooks.Open strFile
'close the 1rst workbook and kill the temporary files
ThisWorkbook.ChangeFileAccess xlReadOnly
Kill strTemporaryFileName
ThisWorkbook.Close False
End Sub
When I open a second workbook when the 1rst one is already open, the ThisExcelApplication_WorkbookOpen Method is logicaly called. It checks the name of the opened workbooks. If it is not the same name, it tries to open a new excel instance and open this workbook on it.
Then i get the error 6 Overflow on this line :
OtherExcelApplication.Workbooks.Open strFichier
Maybe someone have an idea to help me?
Thanks a lot for your attention.
PS: Sorry for my english, it might be bad !

How to copy and paste into another workbook using vba by timer without target workbook popping up each time?

I am copy and pasting data from workbookA to workbookB every 1 minute interval using the application.ontime method. However while I'm running this macro I am working on something different entirely on workbookC. Each time the macro runs, workbookB pops up which interferes with what I'm doing on workbookC. It can get frustrating... is there a way around this?
My code has the structure:
sub dataextract()
ThisWorkbook.Activate 'ThisWorkbook is workbookA
'copy the data
If workbookB Is Nothing Then
Set workbookB= Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= Workbooks("Df.xlsx")
End If
workbookB.Activate
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
end sub
Any suggestions?
sub dataextract()
dim xl as application
dim workbookB as workbook
'ThisWorkbook.Activate 'ThisWorkbook is workbookA, unnecessary to activate workbook when you copy
'copy the data
If workbookB Is Nothing Then
set xl = new application 'create another excel application
xl.visible = false 'set the new excel application invisible
Set workbookB= xl.Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= xl.Workbooks("Df.xlsx")
End If
'workbookB.Activate 'unnecessary to activate workbook when you paste
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
end sub
You have two options to stop the flashing, using the below function, turn off screen updating (the function includes a few other things too, but don't forget to turn it back on).
Sub SetEvents(ByVal State As Boolean)
With Excel.Application
.ScreenUpdating = State
.EnableEvents = State
If State = True Then .Calculation = xlCalculationAutomatic Else .Calculation = xlCalculationManual
End With
End Sub
Or stop using the activeworkbook/select range method. If you define your references properly, ie.
Workbooks("Mybook.xlsx").Sheets("Sheet1").Range("A1").copy
The flashing will stop as the workbooks aren't in fact changing.
If you want to include the first option, as per your comment, add the function outside the sub and call it, as below:
sub dataextract()
setEvents False
ThisWorkbook.Activate 'ThisWorkbook is workbookA
'copy the data
If workbookB Is Nothing Then
Set workbookB= Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= Workbooks("Df.xlsx")
End If
workbookB.Activate
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
setEvents True
end sub
Sub SetEvents(ByVal State As Boolean)
With Excel.Application
.ScreenUpdating = State
.EnableEvents = State
If State = True Then .Calculation = xlCalculationAutomatic Else .Calculation = xlCalculationManual
End With
End Sub

Copying existing password protected sheet to new workbook as an unprotected sheet does not make the new worksheet unprotected

Copying an existing password protected sheet to a new workbook as an unprotected sheet gives the following error when user tries to type in data in the new worksheet.
Error: "the cell or chart you're trying to change is on a protected sheet"
Click OK on the error message.
Please note that this error happens only once. click OK on the pop up error message and type again, then excel allows you to type data in the cells and save the sheet.
We have an excel (format .xls) file currently being used to create another excel spreadsheet when a button on a form in the same spreadsheet is clicked. It basically copies one password protected blank sheet (a template) to a new workbook as an unprotected sheet. The code below used to work with excel 2007(using .xls format). We recently upgraded from excel 2007 to excel 2013 and the problem appeared.
Private Sub cmd_Click()
Dim jBook As Workbook
Dim jsheet As Worksheet
CurrentWorkBook = ActiveWorkbook.Name
Workbooks(CurrentWorkBook).Unprotect jWorksheetPassword
'catch all for errors
On Error GoTo ErrEnd
Dim orginalScreenUpdating As Boolean
orginalScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
If Range("Language").Value = "2" Then
'French
Set jsheet = TemplateFR
Else
'english
Set jsheet = TemplateEN
End If
jsheet.Visible = xlSheetHidden
'jSheet.Visible = xlSheetVisible
'Delete this line
jsheet.Unprotect jWorksheetPassword
Set jBook = Workbooks.Add(xlWBATWorksheet)
jsheet.Copy After:=jBook.Sheets(1)
jBook.Sheets(2).Visible = xlSheetVisible
Application.DisplayAlerts = False
jBook.Sheets(1).Delete
Application.DisplayAlerts = True
jsheet.Visible = xlSheetVeryHidden
'Delete this line
jBook.Sheets(1).Unprotect jWorksheetPassword
'Delete this line
'jsheet.Protect Password:=jWorksheetPassword
NoErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.ScreenUpdating = orginalScreenUpdating
Unload Me
Exit Sub
ErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox DataTable.Range("MSG4").Value, vbCritical, DataTable.Range("MSG4TITLE").Value
Unload Me
End Sub
The following lines of code activate the original workbook and this somehow clears the protection of the copied sheet with excel 2013 only. On Excel 2007 this causes the original workbook to be activated and confuses users, hence the check for 2013.
If Application.Version = "15.0" Then
Workbooks(CurrentWorkBook).Activate
'jBook.Activate
End If
This is a hack that happens to work. If some one finds a better solution please do post it here as well.
The full code listing is as follows:
Private Sub cmd_Click()
Dim jBook As Workbook
Dim jsheet As Worksheet
CurrentWorkBook = ActiveWorkbook.Name
Workbooks(CurrentWorkBook).Unprotect jWorksheetPassword
'catch all for errors
On Error GoTo ErrEnd
Dim orginalScreenUpdating As Boolean
orginalScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
If Range("Language").Value = "2" Then
'French
Set jsheet = TemplateFR
Else
'english
Set jsheet = TemplateEN
End If
jsheet.Visible = xlSheetHidden
Set jBook = Workbooks.Add(xlWBATWorksheet)
jsheet.Copy After:=jBook.Sheets(1)
jBook.Sheets(2).Visible = xlSheetVisible
Application.DisplayAlerts = False
jBook.Sheets(1).Delete
Application.DisplayAlerts = True
If Application.Version = "15.0" Then
Workbooks(CurrentWorkBook).Activate
'jBook.Activate
End If
jsheet.Visible = xlSheetVeryHidden
NoErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.ScreenUpdating = orginalScreenUpdating
Unload Me
Exit Sub
ErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox DataTable.Range("MSG4").Value, vbCritical, DataTable.Range("MSG4TITLE").Value
Unload Me
End Sub

VBA code to go back to master version of Workbook after creating sub-versions

I have a workbook which I use as a Master template, that has multiple variations created with different tabs in each one and sent to a different Distribution List. How can I tell VBA to return to the Master workbook after it saved and sends each 'sub' version? Here is my code for one of those sub versions:
Sub Alignment_Final()
'
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Sheets(Array("T1", "T2", "T3", "T4", "T5", "CS", "Updates", "Branch Updates", "CS Updates", "Control", "BDE", "Termed BBO", "Alignment")). _
Select
Sheets("Branch Updates").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Branch Alignment").Activate
FPath = "C:\Users\mmarshall\Documents\Alignment"
FName = "Alignment" & Format(Date, "dd-mm-yyyy") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=FPath & "\" & FName
Call Mail_To_DL
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
This works just fine but it leaves me in the new version of the workbook. I need to return to the Master workbook (called Master Alignment) after this is created and emailed to create more versions with different worksheets in each . My apologies if this is rudimentary. I'm running Excel 2010 on Windows 7.
If you define you master workbook to a workbook object in the beginning of you code, you can activate that workbook again later.
Define the master workbook like this (assuming that it is your active workbook when you start running your code):
Dim MasterWb As Workbook
Set MasterWb = ActiveWorkbook
And then when you want to activate that workbook again, you can do that by:
MasterWb.Activate