【问题标题】:Excel Auto refresh and save the file into my computer everydayExcel 每天自动刷新并将文件保存到我的电脑中
【发布时间】:2019-05-14 08:06:06
【问题描述】:

我的 excel 文件已连接到分析服务数据库。即使我不打开excel,我也想每天自动刷新excel并将其保存到我的电脑中。

我想知道该怎么做。

我想这样做的原因:因为它连接到数据库,它只显示最近 5 周的数据,我想每天保存所有数据,这样我就可以拥有历史数据。

如果你知道怎么做,请帮忙

我想要的是excel文件自动保存到我的电脑,例如桌面,即使我没有打开它。

【问题讨论】:

  • 你应该在这里搜索一下,这是一个可能相关的结果:stackoverflow.com/a/41740628/4961700
  • 仍然有很多不同,我除外。即使我没有打开 excel 文件,我也希望它自动保存
  • 那么当您进行搜索时,您还发现了哪些其他示例?

标签: excel vba autosave


【解决方案1】:

您可以编写一个 VBA 宏(重复使用您已经编写的内容)并在需要时在 Windows 调度程序启动的 VBS 脚本中从 Excel 调用它。

我的 BAT 文件

::********************************************************************
::* Generate-Excel-File.bat
::********************************************************************

@echo ON
SETLOCAL ENABLEDELAYEDEXPANSION

C:\windows\syswow64\cscript.exe LoadExcel.vbs

我的 VBS 文件

'*****************************************************************************
'* LoadExcel.vbs
'*****************************************************************************

' Create a WshShell to get the current directory

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

' Create an Excel instance
Dim oExcel
Dim oWorkBook

Set oExcel = CreateObject("Excel.Application") 

' Disable Excel UI elements
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
oExcel.FeatureInstall = msoFeatureInstallNone

' Tell Excel what the current working directory is 
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath

strSaveDefaultPath = oExcel.DefaultFilePath
strPath = WshShell.CurrentDirectory 
oExcel.DefaultFilePath = strPath

' Open the Workbook specified on the command-line 

Set oWorkBook = oExcel.Workbooks.Open(strPath & "\US.TRACKING-FILE.NEW.xlsm")

' Build the macro name with the full path to the workbook
on error resume next 
   ' Run the calculation macro
   oExcel.Run "LoadCSV"
   if err.number <> 0 Then
      ' Error occurred - just close it down.
   End If
   err.clear
on error goto 0 

'oWorkBook.Save 

'oExcel.DefaultFilePath = strSaveDefaultPath

' Clean up and shut down
Set oWorkBook = Nothing

' Don’t Quit() Excel if there are other Excel instances 
' running, Quit() will 
' shut those down also
if oExcel.Workbooks.Count = 0 Then
   oExcel.Quit
End If

Set oExcel = Nothing
Set WshShell = Nothing

希望能帮助您解决问题。

在此示例中,我在 Excel 中加载了一个 CSV 文件,但如果您愿意,您可以在 VBA 中运行 SQL 命令并使用纯 Excel 宏填充您想要的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多