vba 我为 excel 编写了一个宏,用于在 Aspen 中进行模拟。宏运行良好,除了一部分不能正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4908032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I have written a macro for excel which is used for a simulation in Aspen. The macro is working well, except one part which is not working as it should
提问by Ordo
I have written a macro for excel which is used for a simulation in Aspen. The macro is working well, except one part which is not working as it should. I don't now why but the part
我为 excel 编写了一个宏,用于在 Aspen 中进行模拟。宏运行良好,除了一部分不能正常工作。我现在不知道为什么 但那部分
For j = 0 To 1
Call Run
Next j
in the Sub Start()
is not executed properly. As the for loop shows the Call Run
should be executed two times. Sincerly this is not the case. When i run the Sub Start() it works, but the Call Run
is only executed one time. Here the complete code of the macro in VB. I would be thankful about any advise.
在Sub Start()
没有正确执行。由于 for 循环显示Call Run
应该执行两次。诚然,事实并非如此。当我运行 Sub Start() 时,它可以工作,但Call Run
只执行一次。这里是VB中宏的完整代码。我会很感激任何建议。
Option Explicit
Dim na As Integer
Dim nb As Integer
Dim nc As Integer
'Dim nd As Integer
Dim i As Integer
Dim j As Integer
'Dim B As Integer
'Dim B As Double
Dim nresults As Integer
Dim Index1 As Integer
Dim Resultfile As String
Dim Resultfolder As String
Dim nlaeufe As Integer
Dim NewBook As Object
Dim ACMObj As Object
Dim nDaten As Integer
Sub Start()
Application.ActiveWorkbook.Save
nlaeufe = Sheet0.Cells(2, 2)
Call Clear_Data
For i = 0 To nlaeufe
'Sheet0.Cells(3, 2) = nlaeufe - i
Set ACMObj = GetObject(Sheet0.Cells(1, 2))
Application.DisplayAlerts = False
ACMObj.Application.Visible = True
Call Count_Daten
Call Add_Daten(ACMObj, nDaten)
For j = 0 To 2
Call Run
Next j
Call Get_Data
If ACMObj.Application.Simulation.State <> "Running" Then
Call Results_newfile
Application.ActiveWorkbook.Save
End If
Next i
End Sub
Sub Get_Data()
' If Sheet0.Cells(Index1 - 1, 2).Value <> ACMObj.Application.Simulation.Time Then
'Sheet0.Cells(Index1, 2).Value = ACMObj.Application.Simulation.Time
Dim nd
For nd = 0 To 152
Sheet0.Cells(32 + nd, 4 + i).Value = ACMObj.Flowsheet.Resolve(Sheet0.Cells(32 + nd, 2)).Value
Sheet0.Cells(187 + nd, 4 + i).Value = ACMObj.Flowsheet.Resolve(Sheet0.Cells(187 + nd, 2)).Value
Next nd
'Index1 = Index1 + 1
'If RunStatus = 1 Then _
' Application.OnTime Now + Sheet0.Cells(4, 2).Value, "Get_Data"
'Else
'End If
End Sub
Sub Count_Daten()
Dim na
While Sheet0.Cells(6 + na, 4) <> ""
na = na + 1
Wend
nDaten = na - 1
End Sub
Sub Add_Daten(ACMObj, nDaten)
Dim nb
Dim B
For nb = 1 To nDaten
Set B = ACMObj.Application.Simulation.Flowsheet.Resolve(CStr(Sheet0.Cells(6 + nb, 3)))
B.Value = Sheet0.Cells(6 + nb, 4 + i).Value
Next nb
End Sub
Sub Run()
Set ACMObj = GetObject(Sheet0.Cells(1, 2))
Application.DisplayAlerts = False
ACMObj.Application.Visible = True
ACMObj.Application.Simulation.runmode = "Steady State"
On Error Resume Next ' In case we are already running
ACMObj.Run (False)
End Sub
Sub Results_newfile()
Resultfolder = Sheet0.Cells(1, 8)
Resultfile = Sheet0.Cells(2, 8)
' Set NewBook = Workbooks.Add(xlWBATWorksheet)
'NewBook.SaveAs (Resultfolder & Resultfile & ".xls")
'NewBook.Activate
'ThisWorkbook.Activate
Sheet0.Select
Range("A1:BA65536").Select
Selection.Copy
' NewBook.Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Call Copy_figure
'NewBook.Save
ThisWorkbook.Activate
End Sub
Sub Clear_Data()
Dim i As Integer
Dim nc As Integer
'i = 100
Sheet0.Select
Range("C32:HH166").Select
Selection.Clear
While Sheet0.Cells(32 + nc, 3) <> ""
'Range(Cells(32 + i, 2), Cells(182 + i, 53)).Select
nc = nc + 152
Selection.Clear
i = i + 152
Wend
Range("A1").Select
End Sub
回答by Diem
You have displayalerts and an "on error resume next" statement, neither of which are 'turned off' afterwards.
您有 displayalerts 和“on error resume next”语句,之后它们都不会“关闭”。
If you temporarily disable these does it give you any error messages or other indication of what might be happening?
如果您暂时禁用这些功能,它是否会给您任何错误消息或其他可能发生的情况的指示?