Excel关闭时运行VBA

Excel关闭时运行VBA,vba,excel,Vba,Excel,我有一个挑战,至少对我来说,我无法应付。有人可以帮助我或建议如何使宏在Excel关闭时运行吗 当Excel通过VBA关闭时,如何使宏运行 Sub Upload0() ' Upload Webpage content Application.OnTime Now + TimeValue("00:00:15"), "Upload0" With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://cet

我有一个挑战,至少对我来说,我无法应付。有人可以帮助我或建议如何使宏在Excel关闭时运行吗

当Excel通过VBA关闭时,如何使宏运行

Sub Upload0()
    ' Upload Webpage content
    Application.OnTime Now + TimeValue("00:00:15"), "Upload0"

    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://cetatenie.just.ro/ordine/articol-11", Destination:=Range("A1"))
        .Name = "CetatenieOrdine"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 1
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

' Deletes empty cells
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp

' Adjust column width and delet useless rows
Rows("1:31").Select
Selection.Delete Shift:=xlUp
Range("B28").Select
Selection.End(xlDown).Select
Rows("17:309").Select
Selection.Delete Shift:=xlUp

End Sub

多谢大家

1:在模块
Public blnClosedVBA中将布尔标志定义为布尔
,并在关闭工作簿之前在VBA例程中将其设置为true。然后在工作簿\u BeforeClose事件处理程序(在此工作簿下)中,执行以下操作:

If blnClosedVBA = True Then 
Cancel = True 'Stops the workbook from closing
'Rest of your code here
blnClosedVBA = False ' Set so next time you try to close the workbook, it actually closes
'Workbook.Close or ThisWorkbook.Close - depends on your answer to my question below
仅当您自己触发关闭事件时,才会运行例程。在例程结束时,将标志设置为False并触发另一个
工作簿。关闭将关闭工作簿

2:应该为哪个工作簿工作?应该是“ThisWorkbook”(运行代码的工作簿)、“ActiveWorkbook”(激活的工作簿)还是其他工作簿

1.通过VBA关闭工作簿时,如何使宏运行

Sub Upload0()
    ' Upload Webpage content
    Application.OnTime Now + TimeValue("00:00:15"), "Upload0"

    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://cetatenie.just.ro/ordine/articol-11", Destination:=Range("A1"))
        .Name = "CetatenieOrdine"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 1
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

' Deletes empty cells
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp

' Adjust column width and delet useless rows
Rows("1:31").Select
Selection.Delete Shift:=xlUp
Range("B28").Select
Selection.End(xlDown).Select
Rows("17:309").Select
Selection.Delete Shift:=xlUp

End Sub
简短回答:你不能那样做。您的代码是工作簿的一部分,只有在Excel中加载工作簿后才能运行。当Excel启动时,您可以将代码移动到一个单独的外接程序工作簿中,默认情况下选择加载该工作簿(使用“Excel选项|外接程序”)。但是Excel仍然需要在您的计算机上的某个地方运行

您可以编写一个完全独立的程序来完成您想要的任务,将web查询的结果写入Excel工作簿。在这里,我假设您希望工作簿在您自己(当然是在运行Excel时)或其他资源引用时始终包含最新数据。如果您可以获得Visual Basic 6的副本(在法律上可能无法实现),那么这在语法上与VBA基本相同。其次是VB.Net。这在技术上有点复杂

2.此外,我还无法使此宏仅适用于一个工作簿,而不适用于活动工作簿:

我们可以处理的是:这一行:

With ActiveSheet.QueryTables.Add(Connection:= _

这意味着以下代码将始终针对具有焦点的工作表运行(即“活动”-如果您键入某些内容,将接收键盘输入的工作表)。这就是
ActiveSheet
所做的。尝试用类似于此工作簿的内容替换
`ActiveSheet。Sheet1
,其中
Sheet1`是您在VBA编辑器的“属性”窗口中看到的工作表的名称,其中显示“(名称)”。

1。你说的是工作表还是工作簿?宏是否与相关工作表有关?2.我认为您应该尝试手动格式化代码,因为{}代码括号不会使代码更可读。如果工作簿关闭,您打算从何处运行此宏。VBScript?VB.Net?还有什么?很抱歉,您是对的:对于关闭时的工作簿:工作簿名称为Book1.xli我正在VBA中尝试此代码存在于何处?在Book1.xls工作簿中?还是在另一本工作簿中?嗨,马特,谢谢你!我只需要Book1.xls,因为我在模块中编写了宏。当我将宏移动到此工作簿时,它不起作用。是的,您应该将当前代码保留在模块中,然后在此工作簿中,您需要修改BeforeClose事件。在那里,您可以调用Upload0子-将我回答中的
行中的
其余代码替换为
Upload0
。要确认,您希望在通过VBA关闭工作簿时运行代码(即使用ThisWorkbook.Close)。您不希望在工作簿已关闭时运行代码…?不,我需要它同时工作:打开和关闭工作簿。但是,问题是,如果打开工作簿,则代码会尝试为我正在查看或处理的工作簿运行宏,即active,但我只需要它为1个特定文件Book1.xls执行此操作。您将无法在关闭的工作簿中运行此操作(除非先打开它)。如果希望查询表显示在特定工作簿中,请将add语句的第一行更改为:
With Workbooks(“Book1.xls”).Sheetname(“Sheetname”).QueryTables.add(…
),并将目标更改为
Workbooks(“Book1.xls”).Sheetname(“Sheetname”).Range(“A1”)
嘿,迈克,还有一个问题,例如,如果我打开了另一个excel,或者我正在使用另一个文件,那么宏将针对我正在使用的文件运行,如果没有正确提到工作表名称,则会阻塞。。。