VBA Excel代码,excel.exe不会关闭

标签 vba excel

我已经尝试让这段代码工作三天了,但我无法弄清楚。

此代码在外部应用程序中运行并调用 Microsoft Excel 14 库。

我将文件夹中的所有 CSV 文件合并到一个 xlsx 文件中,然后在每个工作表中添加一个图表以对应于其中的数据。但是当我添加图表时,我无法关闭 Excel.exe,导致我尝试再次运行代码时出现问题。

我可以正常运行代码一次,然后出现运行时错误 91“对象变量或 block 变量未设置。 如果我调试代码,它会提示 ActiveSheet.Columns("A:F").AutoFit。

我认为这是因为 Excel 没有正确关闭。我仍然可以在任务管理器中看到 Excel.exe,我需要结束任务才能使其再次工作。 我对 VBA 非常陌生,所以我可能缺少关闭对象的正确方法。

*好的,这里是一个小一点的代码。

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

'Setup workbooks
Dim wB As Excel.Workbook
Dim wBM As Excel.Workbook

'Csv files folder
Dim CSVfolder As String
CSVfolder = "C:\Dynamics\HistDataReport\12-10-14"

'Master Excel file path
Dim mF As String
mF = "C:\Dynamics\HistDataReport\NewWS.xlsx" 'Where your master file is

'open the master file
Set wBM = xlApp.Workbooks.Open(mF)

'search and open the client files
Dim fname As String
fname = Dir(CSVfolder & "\*.csv")
Do While fname <> ""
   'open the client file
   Set wB = xlApp.Workbooks.Open(CSVfolder & "\" & fname)
   'copy the first sheet from client file to master file
   wB.Sheets(1).Copy After:=wBM.Sheets(wBM.Sheets.Count)
   'Add Graph
   ActiveSheet.Columns("A:F").AutoFit
   ActiveSheet.Range("B1:B673").Select
   Charts.Add
   ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
   'save master file
   wBM.Save
   'close client file
   wB.Close False
   'move to next client file
   fname = Dir()
Loop

xlApp.Visible = True
Set xlApp = Nothing

如果我把所有这些都注释掉,那就行了。但我没有得到我需要的图表。

我从这里得到了这个代码 Stackoverflow Code

这是我在解决这个问题时将使用的原始代码

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Dim wsht As Excel.Worksheet
Dim strFileName As String
Dim lastRow As Integer


Set xl = New Excel.Application
xl.DisplayAlerts = False
Set wbk = xl.Workbooks.Add("C:\Dynamics\HistDataReport\NewWS.xlsx")


wbk.SaveAs FILEPATH & Filename2
wbk.Close
Set xl = Nothing


Dim wB As Excel.Workbook
Dim wBM As Excel.Workbook
Dim myChart, As Chart, cht As ChartObject
Dim rngChart As Range, destinationSheet As String

'Csv files folder
Dim CSVfolder As String
CSVfolder = FILEPATH
'Master Excel file path
Dim mF As String

'mF = Application.CurrentProject.path & "\Master.xlsx" 'Where your master file is

 mF = FILEPATH & Filename2
'open the master file
 Set wBM = xlApp.Workbooks.Open(mF)
'search and open the client files
 Dim fname, wS As String
 fname = Dir(CSVfolder & "\*.csv")

 Do While fname <> ""
'open the client file
 Set wB = xlApp.Workbooks.Open(CSVfolder & "\" & fname)
'copy the first sheet from client file to master file
 wB.Sheets(1).Copy After:=wBM.Sheets(wBM.Sheets.Count)
 ActiveSheet.Columns("A:F").AutoFit
 destinationSheet = ActiveSheet.Name
 Set myChart = Charts.Add
 Set myChart = myChart.Location(Where:=xlLocationAsObject, Name:=destinationSheet)
 myChart.SetSourceData Source:=ActiveSheet.Range("B1:B673"), PlotBy:=xlColumns
 myChart.ChartType = xlLine
 ActiveSheet.ChartObjects(1).Activate
 Set cht = ActiveChart.Parent
 Set rngChart = ActiveSheet.Range("H2:Q15")
 cht.left = rngChart.left
 cht.Top = rngChart.Top
 cht.Width = rngChart.Width
 cht.Height = rngChart.Height
 myChart.HasTitle = True
 myChart.ChartTitle.Text = "Week"
 myChart.HasLegend = False
 ActiveSheet.Range("A2").Select


 wBM.Save
 wB.Close False
'move to next client file
 fname = Dir()
 Loop
 wB.Close
 wBM.Close
 Set xlApp = Nothing

如有任何帮助,我们将不胜感激!

最佳答案

我相信您需要 xlApp.Quit 和设置 xlApp= Nothing。

我有一些与隐藏的 Excel 工作簿一起使用的代码,并且在执行任何操作之前我在开始时都有这段代码,以防万一代码的先前迭代仍然存在。

    'Switch off the error handling and the display alerts to avoid any error messages if the old workbook has
      'never been opened and the hidden instance does not exist
    Application.DisplayAlerts = False
   On Error Resume Next
        book.Close SaveChanges:=False
        app.Quit
        Set app = Nothing
    Application.DisplayAlerts = True

关于VBA Excel代码,excel.exe不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27408164/

相关文章:

vba - Excel VBA 未保存在当前目录中

vba - 条件格式,虽然应用但没有出现格式

VBA Word 格式化 Selection.typetext

vba - 选择最后 1 行并自动填充到最后 +1 行

vba - Excel VBA - 范围及其子范围的并集

c# - 打开 XML : How to add rows and cell value after appending new sheet in existing Excel file using c#

java - 使用java将数据从数据库导入到Excel?

Excel , 多查找/匹配公式

java - 日本时代 "Reiwa"支持 Microsoft Excel 日期格式

excel - 使用 Excel VBA 获取工作表名称