Excel应用程序WindowState不行为(VBA)

时间:2014-03-21 16:40:00

标签: excel-vba vba excel

我在VBA中通常用于在后台最小化和运行进程的代码是:

 With Excel.Application
    .ScreenUpdating = False
    .Calculation = Excel.xlCalculationManual
    .EnableEvents = False
    .DisplayAlerts = False
    .WindowState = xlMinimized
 End With

由于某种原因,应用程序窗口会在代码运行时保持交替恢复和最小化,而不是保持最小化。我认为它与数据刷新或将数据复制到文本文件进程有关。当然我想让它在后台运行最小化。我不想隐藏应用程序。以下是我认为导致问题的代码:

Sub StockUpdate()
'
' Rainmeter File Macro
'
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
'
With Excel.Application
    .ScreenUpdating = False
    .Calculation = Excel.xlCalculationManual
    .EnableEvents = False
    .DisplayAlerts = False
    .WindowState = xlMinimized
End With
Calculate
ActiveWorkbook.RefreshAll
Application.Wait (Now + TimeValue("0:00:10"))
Calculate
Application.Wait (Now + TimeValue("0:00:03"))
Sheets("Main").Activate
Range("C16:C25").Select
Selection.Copy
Range("C27:C36").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("C27:C36").Select
Selection.Copy
Set wb = Application.Workbooks.Add
wb.Worksheets(1).Paste
wb.SaveAs Filename:= _
        "[filepath]\Test.txt", FileFormat:= _
        xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
If On_Timer = 1 Then Call Duration
End Sub

数据刷新部分是设置为在后台运行的外部Web查询,上面的代码是子例程,因此在顶部进行的Excel.Application更改将在其他位置重置。我很感激任何建议。感谢

1 个答案:

答案 0 :(得分:0)

尝试类似:

application.windows(thisworkbook.name).windowstate = xlMinimized

你只能选择你能看到的东西,所以当然会再次弹出窗口,而是避免使用select和激活并格式化你的代码,如:

Sheets("Main").Range("C27:C36").value = Sheets("Main").Range("C16:C25").value
Set wb = Application.Workbooks.Add
windows(wb.name).windowstate=xlminimized
thisworkbook.sheets("Main").Range("C27:C36").copy wb.sheets(1)
wb.SaveAs Filename:= _
    "[filepath]\Test.txt", FileFormat:= _
    xlText, CreateBackup:=False
wb.Close savechanges:=False