Excel - refresh data on specific time using VBscript - excel

Hy all.
I need some help. I will be very grateful for any help.
I don't know about using VBscript in excel.
I am using excel 2016. I have a workbook which has connections to MySQL for query. I want that the data connections in work book may be refreshed at specific time intervals.
I try this code:
Private Sub Workbook_Open()
Application.OnTime TimeValue("13:55:00"), "MyMacro"
End Sub
Sub MyMacro()
ActiveWorkbook.RefreshAll
End Sub
The workbook is opened but don't refresh data.
Can some one please guide me for adding or editing code for this purpose.

See http://www.snb-vba.eu/VBA_Application.OnTime_en.html
Try
Private Sub Workbook_Open()
Application.OnTime TimeValue("13:55:00"), "Module1.MyMacro"
End Sub

Related

Workbook_Open() does not work with Workbook Protection

I am using Excel 2016 and have this code written in the ThisWorkbook object in VBA:
Private Sub Workbook_Open()
ThisWorkbook.Protect (password = "password")
End Sub
It is not working. The point here is that this should prevent users from touching the Power Query functions in this workbook. I have saved it as a Macro-enabled workbook, I have all macros and events enabled and this is the only workbook open.
I also have this additional code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Protect (password = "password")
ThisWorkbook.Save
End Sub
And that is not working either. It works fine if I insert that "ThisWorkbook.Protect" code into a general module or worksheet object and run it manually, but when I want this particular excel file to run this code automatically on open or close, it does not do it.
Any ideas what could be causing this?
For some reason running ThisWorkbook.Protect on a protected workbook seems to unprotect it (even though I couldn't find any documentation that says that it does this) so try this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ThisWorkbook.ProtectStructure Then ThisWorkbook.Protect Password:="password"
ThisWorkbook.Save
End Sub
In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module.
If an Auto_Open, Auto_Close, or other automatically-running subroutine is stored "behind" a worksheet or ThisWorkbook, it may not function correctly when you open or close your workbook, or when you perform an action that should cause the subroutine to run.
MS Topic Discussion - Code "Behind" a Workbook

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

Run macro when user "locks" windows

I'd like to save my spreadsheet every time I lock my windows pc. Is there a built-in trigger in excel (similar to on-close)? Or might I be able to get a macro to run every minute and check if my machine is in use or locked?
If you really need this you can try the following workaround:
Enable auditing for the lock event in the event viewer, so that event 4800 for locking is logged, as described here.
Create a scheduled task triggered on the lock event. Choose Trigger on Event with Protocol: "Security", Source: "Microsoft-Windows-Security-Auditing" and Event-ID: "4800"
Run a vbscript that uses GetObject(, "Excel.Application") to retrieve the currently active excel session and run a macro that saves for you with Application.Run (like in this example)
Not very straight forward, but on the other hand, the easy way would just be to press ctrl+s everytime before you lock your computer.
You could try this and see if this works instead of using a macro:
Click the Microsoft Office Button , and then click Excel Options.
Click Save.
Select the Save AutoRecover information every x minutes check box.
In the minutes list, specify how often you want the program to save your data and the program state.
But if you specifically want a macro I could try to form one for you.
Okay well add this into a standard module:
Option Explicit
Public RunTime
Sub StartTimer()
RunTime = Now + #12:10:00 AM#
Application.OnTime RunTime, "SaveBook", schedule:=True
End Sub
Sub SaveBook()
ActiveWorkbook.Save
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime RunTime, "SaveBook", schedule:=False
End Sub
Place this in your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Then add this into your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
StopTimer
End Sub
Have fun with it and fiddle around with it

Excel VBA, refresh data on opening if not refreshed today already

I have an excel spreadsheet that uses pivot tables.
Currently I have a hyperlink to refresh the data which is new each day.
I would like to develop a method of refreshing the data on opening the Excel file, but not it it has already been updated today.
Is this possible using VBA?
My current code is...
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$A$1" Then
MsgBox "Your data will now be updated. This may take a moment..."
ActiveWorkbook.RefreshAll
Sheet1.Range("A2").Value = DateTime.Now
Exit Sub
End If
End Sub
Thanks in advance.
Try to put your code in Workbook_Open
http://support.microsoft.com/kb/265113

Saving Personal.xls automatically

I use Excel 2003 on a Windows 7 Professional setup.
In my Personal.xls file, I have compiled snippets of code/formulae that I have picked up from different places, to be available for ready reference when I use Excel. These contain a number of volatile functions such as cell(), rand(), today() etc. As a result, when I close Excel, it asks me whether I would like to save Personal.xls.
I would like to keep my Personal.xls as it is, and yet disable the popup somehow. I am fine with saving, not saving, either way, as I won't be changing Personal.xls.
I have tried the following code in my personal.xls in the Workbook_BeforeClose section
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
But it doesn't seem to work. I have tried some variations, including .Save and .Saved=True, and also tried to alternatively use ThisWorkbook., Me. & Workbooks("PERSONAL.xls"). However, Excel still asks me save Personal.xls
I have also tried to disable calculations in my Personal.xls viz.
Private Sub Workbook_Open()
For Each ws In ThisWorkbook.Worksheets
ws.EnableCalculation = False
Next
End Sub
This doesn't solve the problem either. Finally I tried to do a 'ThisWorkbook.Save' after changing calculation mode to manual, but that doesn't change anything either.
Am I missing something here? Any advice would be appreciated. Thanks in advance!
You want:
Me.Save
In your workbook BeforeClose event.
Why not just force-shut the document?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close savechanges:=True
End Sub
For what I was trying to do I am actually using savechanges:=False. Both ways worked.
I just remove the Cancel As Boolean
Private Sub Workbook_BeforeClose()
ThisWorkbook.Save
End Sub

Resources