VBA Excel代码,excel.exe将无法关闭

时间:2014-12-10 18:23:36

标签: excel vba excel-vba

我一直试图让这段代码工作3天,但我无法弄明白。

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

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

我可以运行一次代码,然后我得到一个运行时错误91"对象变量或块变量未设置。 如果我调试代码,它会抱怨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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

我相信你需要xlApp.Quit和set 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
相关问题