Windows("ETY Tracker V1.2.xlsm").ScreenUpdating = False - vba

Could you please help to solve the problem I am facing,when i run the program its throwing me an error "Object dosen't support this property or method"
Private Sub Workbook_Open()
'Application.Visible = False
'Application.ScreenUpdating = False
Windows("ETY Tracker V1.2.xlsm").ScreenUpdating = False
UserForm1.Show
End Sub
please help...I also tried both application .visible but it will close all the active workbooks we have, and application.screenupdating will not allow me to edit or open any other sheet other than the present user form.

Are you trying to hide the workbook and just show the form?
Private Sub Workbook_Open()
ThisWorkbook.Windows(1).Visible = False
UserForm1.Show
End Sub
Or hide Excel and show the form? This won't close the workbooks - they're just inside the application which in itself is hidden.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub

Excel.Application.ScreenUpdating doesn't stop users from interacting with Excel, it just stops them from seeing the interaction.
Try Application.Interactive = False, but remember to set it back to true or you will have to close your excel application.

Related

How to prevent any changes in code and be able at same time to look at it? (VBA)

Is there, by chance, any way to prevent from changing code and simultaneously be able to look at it?
The purpose is introductory, so that user could look at code without ability to do any changes.
Thank you in advance
Here is a tricky one. You can add the following three subscripts and it will make the file Read Only and also stop anyone from saving the workbook unless they use the SaveForReal Subscript/Macro.
Inside the ThisWorkbook VBA Object:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Saved = True
Cancel = True
End Sub
Inside a Module Object:
Private Sub SaveForReal()
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
To save the workbook, you need to open the VBA editor and Run the SaveForReal Subscript, otherwise the Save Button and Save As button does nothing.
Edit: Added On Open Read Only Change.

starting a userform without excel in the background

I have this vba code where it has a userform.
Now I would like to start the userform; where the people can't see the Worksheets.
I have added this in my code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True End Sub
Then in the "ThisWorkbook; I have added this:
Private Sub Workbook_Open()
Application.Visible = False ' only for final version
UserForm1.Show
'enter code here
End Sub
The thing is when I upload this .xlsm to a website, and when you open it; it does not start the userform right away. It somehow goes to an error. If you have an excel open.
Not sure how to protect the file, without having the endusers altering the file
Any ideas?
Maybe this helps:
In "ThisWorkbook" under "Open" add:
Private Sub Workbook_Open()
Application.Visible = False
UserForm2.Show vbModeless
End Sub
This will hide Excel and execute UserForm2.
BTW: To end your program add this code (e.g. to one of your Userform Buttons):
Private Sub CommandButton1_Click()
Application.Quit
End Sub
It will close Excel.

Preventing Excel prompt from Word VBA

I'm working with VBA on Word and Excel. I have the Word running a userform which will automatically open an Excel file. User should fill some data in Excel file and then go back to the Word userform. When user finish filling all fields in Word userform, it will run some VBA code on Word that copy data from Excel to Word. After finished, the Excel file will be closed automatically. Therefore, I need to prevent user from closing the Excel app manually.
In order to do that, I use these code in Excel VBA in Sub Workbook_BeforeClose. If user close the Excel application window, it will show a message box that ask whether the user is still working with the Word userform. The code as follows:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
answer = MsgBox("Are you still working with Word userform?", vbYesNo)
If answer = vbYes Then
Cancel = True
MsgBox "This workbook should not be closed. It will be automatically closed when you finish working with Ms. Word Template Userform."
Else
Application.ThisWorkbook.Saved = True
End If
End Sub
In the Word VBA, I have code to close the Excel file:
Sub closeExcelApp()
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
End Sub
This Sub will be called when the Word VBA code done copying data from Excel to Word. However, calling this Sub will cause the Workbook_BeforeClose called. Meanwhile, I don't want the Workbook_BeforeClose called when I call this closeExcelApp sub from Word VBA.
Any suggestion?
You can just disable events:
Sub closeExcelApp()
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelApp.EnableEvents = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
End Sub
As explained in comments, add this line on top of the modules : Public ClosingFromWord As Boolean and set this boolean to False when you start your code execution.
As you are working between apps, the easiest way will be to write the value of the boolean in a cell in Excel from Word and read/laod this value in the Workbook_BeforeClose to avoid going through the whole code of that routine.
And modify you code to look like this :
Sub closeExcelApp()
ClosingFromWord = True
excelApp.Workbooks(1).Sheets(1).Cells(1,1) = ClosingFromWord
If Not excelApp Is Nothing Then
excelApp.DisplayAlerts = False
excelWb.Close savechanges:=False
Set excelWb = Nothing
excelApp.Quit
End If
Set excelApp = Nothing
ClosingFromWord = False
excelApp.Workbooks(1).Sheets(1).Cells(1,1) = ClosingFromWord
End Sub
So while you execute closeExcelApp, the boolean will be set to True and the Workbook_BeforeClose won't be executed in his totality as it'll be exited with If ClosingFromWord Then Exit Sub :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ClosingFromWord = Workbooks(1).Sheets(1).Cells(1,1)
If ClosingFromWord Then Exit Sub
answer = MsgBox("Are you still working with Word userform?", vbYesNo)
If answer = vbYes Then
Cancel = True
MsgBox "This workbook should not be closed. It will be automatically closed when you finish working with Ms. Word Template Userform."
Else
Application.ThisWorkbook.Saved = True
End If
End Sub

Menu to navigate through sheets doesn't work proper

I've created a menu to navigate through sheets. But when I use one of the buttons to go to another sheet and type something in this another sheet the things that I type appear in the first one. Although when I use the tabs to jump to one sheet to another it works fine. Seems to be that the macro is considering a relative reference instead a absolute one.
Here's what my macro does (or at least should):
Private Sub cadastrar_clientes_Click()
Application.ScreenUpdating = False
Menu.Hide
Sheets("Clientes").Select
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Select
End Sub
Notes:
I've tried to use ".activate" instead of ".select".
I've tried to use only "Sheets("Clientes").Select", even so the bug occurs.
To execute "Excel /unregserver" on the Windows CMD solved my problem once, but I couldn't do this again. It doesn't seems to work anymore.
I did a menu like this one once, but I didn't had these problems.
When the workbook opens the following code is executed:
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
End Sub
That's it. I hope that somebody could help me.
Replace all code on VBA module Menu with this (tested in the file you provided)
Option Explicit
Private Sub fechar_menu_Click()
Menu.Hide
End Sub
Private Sub novo_pedido_Click()
goToTab ActiveWorkbook.Worksheets("Novo pedido"), "B5"
End Sub
Private Sub ver_pedidos_Click()
goToTab ActiveWorkbook.Worksheets("Consultar pedido"), "F1"
End Sub
Private Sub cadastrar_clientes_Click()
goToTab ActiveWorkbook.Worksheets("Clientes")
End Sub
Private Sub cadastrar_produtos_Click()
goToTab ActiveWorkbook.Worksheets("Produtos")
End Sub
Private Sub cadastrar_transportadoras_Click()
goToTab ActiveWorkbook.Worksheets("Transportadoras")
End Sub
Private Sub painel_financeiro_Click()
goToTab ActiveWorkbook.Worksheets("Painel Financeiro"), "B3"
End Sub
Private Sub goToTab(ByRef ws As Worksheet, Optional cel As String = vbNullString)
Menu.Hide
Application.ScreenUpdating = False
With ws
.Activate
If Len(cel) = 0 Then
.Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Select
Else
.Range(cel).Select
End If
End With
Application.ScreenUpdating = True
End Sub
Well, I guess I know a lot more about the problem now. I've tested the workbook over various computers and came to the conclusion that the problem is within the Office 2013. I haven't seem this issue when running the 2007 or 2010 version. So far I haven't come to a solution. I would like to thanks Paul Bika for helping.

Worksheet.RefreshAll triggers Worksheet_changed Event - how do I stop this from happeing?

I have an excel worksheet that is being used as a database front end for access. When a user changes data in a cell it will run the Worksheet_changed event to run codes that updates the access database.
However - I also have a refresh button that, you guessed it, refreshes the spreadsheet. This also causes the worksheet_changed event to run which will sometimes error out the program.
Private Sub RefreshButton_Click()
Refreshbuttons
ActiveWorkbook.RefreshAll
End Sub
How do I stop the work sheet changed event from happening when the refresh button is pressed? I have tried a Boolean flag which will stop the worksheet changed event from running when refreshed button is pressed - but it stops it from running at all (example of what I did)
Private Sub RefreshButton_Click()
Dim Flag as Boolean
Flag = True
Refreshbuttons
ActiveWorkbook.RefreshAll
Flag = False
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Flag = true then
Exit Sub
Else
[...] {Rest of code below here}
I am stuck - any help is greatly appreciated!!!
Thanks,
Ethan
EDIT
Thanks Tim! you pointed me in the right direction. I ended up going with (code below) and it worked beautifully. I appreciate everyones help!
Private Sub RefreshButton_Click()
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
Else
End If
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
MsgBox "Refresh Complete"
End Sub
Private Sub RefreshButton_Click()
On Error Goto haveError
Refreshbuttons
Application.EnableEvents = False
ActiveWorkbook.RefreshAll
haveError:
Application.EnableEvents = True
End Sub
Declare Flag as global like below. It should work.
Public Flag as Boolean
All office applications have a built-in function to control this behavior. Simply add:
Application.EnableEvents = False to the beginning of the RefreshButton_Click event and
Application.EnableEvents = True to the end of the event.