VBA程序后Excel应用程序无法使用

时间:2018-06-09 01:27:15

标签: excel vba ms-project

我在 MS Project 中有一个VBA程序,它打开Excel应用程序和电子表格,稍微调整电子表格,然后将其保存为新文件,以便将其导入MS项目。

代码工作得很好,它像我想要的那样修改Excel文件,但是在运行代码之后,我打开Excel并且它不再可用,它只是一个空白的应用程序。

起初我没有关闭应用程序或再次显示它(targetApp.QuittargetApp.Visible=True),我可能会导致问题,但添加这两行代码并没有#39;解决问题。任何帮助或指示都将不胜感激,谢谢。

以下空白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

screenshot

1 个答案:

答案 0 :(得分:1)

检查任务管理器,确保Excel的实例未继续运行且未正确关闭。

如果您未使用$(document).on("load","#print_frame",function(){ var iframeHeight = $(this).contents().height(); console.log(iframeHeight); // Check the console for that value. $(this).height(iframeHeight); }); 退出VBA打开的Excel实例,它将保持活动状态 - 但隐藏 - 并且您的实例生成的任何对话框都会停止尝试打开新实例