Excel 应用程序在 VBA 程序后无法使用
Excel Application Unusable after VBA Program
我在 MS Project 中有一个 VBA 程序,它会打开 Excel 应用程序和一个电子表格,稍微调整电子表格,然后将其另存为一个新文件,以便它可以导入到 MS 项目中。
代码完美运行,它修改了 Excel 文件,就像我想要的那样,但是在 运行 代码之后,我打开 Excel 它不再可用,它只是一个空白的应用程序。
起初我没有关闭应用程序或使其再次可见(targetApp.Quit
和 targetApp.Visible=True
),我认为这可能是导致问题的原因,但是添加这两行代码并没有'解决问题。任何帮助或指导将不胜感激,谢谢。
以下空白Excel应用程序的代码和屏幕截图:
Private Sub processExportExcelSheet()
If Not ErrorMessage = "" Then Exit Sub
Dim targetApp As New Excel.Application
Dim targetWorkBook As Excel.Workbook
Dim targetSheet As Excel.Worksheet
targetApp.Visible = False
targetApp.DisplayAlerts = False
targetApp.ScreenUpdating = False
Set targetWorkBook = targetApp.Workbooks.Open("C:\Exports\Data.xls")
Set targetSheet = targetWorkBook.Worksheets(1)
Dim rowCount, columnCount As Integer
For rowCount = 1 To 10
For columnCount = 1 To 10
If targetSheet.Cells(rowCount, columnCount) = "P" _
Then GoTo exitLoop
Next columnCount
Next rowCount
ErrorMessage = "The data is corrupt"
GoTo corruptData
exitLoop:
rowCount = rowCount - 1
columnCount = columnCount - 1
If Not rowCount = 0 Then targetSheet.Range(targetSheet.Rows(1), _
targetSheet.Rows(rowCount)).Delete Shift:=x1Up
If Not columnCount = 0 Then targetSheet.Range(targetSheet.Columns(1), _
targetSheet.Columns(columnCount)).Delete Shift:=x1ToLeft
Dim count, deleteCount As Integer
count = 1
deleteCount = 0
Do While deleteCount < 5
If targetSheet.Cells(1, count) = "" Then
targetSheet.Columns(count).Delete Shift:=x1ToLeft
deleteCount = deleteCount + 1
Else
targetSheet.Cells(1, count) = "Title" & count
count = count + 1
deleteCount = 0
End If
Loop
count = 2
deleteCount = 0
Do While deleteCount < 5
If targetSheet.Cells(count, 1) = "" Then
targetSheet.Rows(count).Delete Shift:=x1Up
deleteCount = deleteCount + 1
Else
count = count + 1
deleteCount = 0
End If
Loop
targetWorkBook.SaveAs Filename:="C:\Exports\Data.xlsx", FileFormat:=51
corruptData:
targetWorkBook.Close
Kill ("C:\Exports\Data.xls")
targetApp.ScreenUpdating = True
targetApp.DisplayAlerts = True
targetApp.Visible = True
targetApp.Quit
End Sub
检查任务管理器并确保 Excel 的实例没有被遗留 运行 并且没有被正确关闭。
如果您还没有退出 VBA 打开的 Excel 的 .Quit
实例,它将保持活动状态 - 但隐藏 - 并且您的实例生成的任何对话框都会停止尝试打开新实例
我在 MS Project 中有一个 VBA 程序,它会打开 Excel 应用程序和一个电子表格,稍微调整电子表格,然后将其另存为一个新文件,以便它可以导入到 MS 项目中。
代码完美运行,它修改了 Excel 文件,就像我想要的那样,但是在 运行 代码之后,我打开 Excel 它不再可用,它只是一个空白的应用程序。
起初我没有关闭应用程序或使其再次可见(targetApp.Quit
和 targetApp.Visible=True
),我认为这可能是导致问题的原因,但是添加这两行代码并没有'解决问题。任何帮助或指导将不胜感激,谢谢。
以下空白Excel应用程序的代码和屏幕截图:
Private Sub processExportExcelSheet()
If Not ErrorMessage = "" Then Exit Sub
Dim targetApp As New Excel.Application
Dim targetWorkBook As Excel.Workbook
Dim targetSheet As Excel.Worksheet
targetApp.Visible = False
targetApp.DisplayAlerts = False
targetApp.ScreenUpdating = False
Set targetWorkBook = targetApp.Workbooks.Open("C:\Exports\Data.xls")
Set targetSheet = targetWorkBook.Worksheets(1)
Dim rowCount, columnCount As Integer
For rowCount = 1 To 10
For columnCount = 1 To 10
If targetSheet.Cells(rowCount, columnCount) = "P" _
Then GoTo exitLoop
Next columnCount
Next rowCount
ErrorMessage = "The data is corrupt"
GoTo corruptData
exitLoop:
rowCount = rowCount - 1
columnCount = columnCount - 1
If Not rowCount = 0 Then targetSheet.Range(targetSheet.Rows(1), _
targetSheet.Rows(rowCount)).Delete Shift:=x1Up
If Not columnCount = 0 Then targetSheet.Range(targetSheet.Columns(1), _
targetSheet.Columns(columnCount)).Delete Shift:=x1ToLeft
Dim count, deleteCount As Integer
count = 1
deleteCount = 0
Do While deleteCount < 5
If targetSheet.Cells(1, count) = "" Then
targetSheet.Columns(count).Delete Shift:=x1ToLeft
deleteCount = deleteCount + 1
Else
targetSheet.Cells(1, count) = "Title" & count
count = count + 1
deleteCount = 0
End If
Loop
count = 2
deleteCount = 0
Do While deleteCount < 5
If targetSheet.Cells(count, 1) = "" Then
targetSheet.Rows(count).Delete Shift:=x1Up
deleteCount = deleteCount + 1
Else
count = count + 1
deleteCount = 0
End If
Loop
targetWorkBook.SaveAs Filename:="C:\Exports\Data.xlsx", FileFormat:=51
corruptData:
targetWorkBook.Close
Kill ("C:\Exports\Data.xls")
targetApp.ScreenUpdating = True
targetApp.DisplayAlerts = True
targetApp.Visible = True
targetApp.Quit
End Sub
检查任务管理器并确保 Excel 的实例没有被遗留 运行 并且没有被正确关闭。
如果您还没有退出 VBA 打开的 Excel 的 .Quit
实例,它将保持活动状态 - 但隐藏 - 并且您的实例生成的任何对话框都会停止尝试打开新实例