Hysys Excel Automation Training

Thursday, June 9, 2011

Hysys Tutorial to Link Hysys Streams on Excel



How to link Hysys Simulation to Excel?
Open Example1.HSC. This file is generated in Aspen Hysys V7.1, may not work in previous version of Hysys, such as Hysys 2006 or Hysys 3.2. If you have older version of Hysys, make your own simulation with two streams named FEED1 and FEED2. Here is some operating condition of the streams.


Linking between Hysys Simulations and Excel using Macro VBA (Visual Basic Application). Create new module in Macro VBA and put this code.

Option Explicit

Public hyApp As HYSYS.Application
Public simCase As SimulationCase
' LOADING HYSYS TUTORIAL FILE
Set hyApp = CreateObject("HYSYS.Application")    hyApp.Visible = True    Set simCase = hyApp.ActiveDocument    If simCase Is Nothing Then        fileName = Worksheets("inputhysys").Range("c2")                 If fileName <> "False" And simCase Is Nothing Then            Set simCase = GetObject(fileName, "HYSYS.SimulationCase")            simCase.Visible = True        End If    End If 

Note: I put directory address of Hysys file (HSC file) in cell C2. And the address is H:\BLOG\hysys\example1.hsc.
PUT HSC FILE IN YOUR DIRECTORY, MAKE SURE YOU EDIT  EDIT THE  DIRECTORY ADDRESS, OR HYSYS FILE WILL NOT BE LOADED

How to Access Hysys Streams from Excel?
Put this code to make link to Hysys Streams
Public FEED1 As ProcessStream, FEED2 As ProcessStream
Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")
How to Export Data from Excel To Hysys Simulation?
How to Import Data from Hysys Streams to Excel File?
Write down this code to export and import data from and to excel



Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")Worksheets("Sheet1").Range("D6").Value = FEED1.MassFlow.GetValue("LB/HR")       ' import data MASS FLOWRATE from hysys streamsWorksheets("Sheet1").Range("D7").Value = FEED1.Pressure.GetValue("PSIG")       ' import data PRESSURE from hysys streamsWorksheets("Sheet1").Range("D8").Value = FEED1.Temperature.GetValue("C")       ' import data TEMPERATURE from hysys streams


Set FEED2 = simCase.Flowsheet.MaterialStreams.Item("FEED2")    PRESSFEED2 = Worksheets("Sheet1").Range("H7").Value    FEED2.Pressure.SetValue PRESSFEED2, "PSIA"         ' export pressure    FLOWFEED2 = Worksheets("Sheet1").Range("H6").Value    FEED2.MolarFlow.SetValue FLOWFEED2, "MMSCFD"      ' export molar flowrate    TEMPFEED2 = Worksheets("Sheet1").Range("H8").Value    FEED2.Temperature.SetValue TEMPFEED2, "F"           ' export temperature

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hysys file can be found here
Excel file can be found here

MAKE SURE TO PUT CORRECT DIRECTORY ADDRESS OF HYSYS FILE IN CELL

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here is complete code from example.xls

Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public FEED1 As ProcessStream, FEED2 As ProcessStream




Public Sub StartHYSYS()
Dim fileName As String
Dim PRESSFEED2 As Double, FLOWFEED2 As Double, TEMPFEED2 As Double
    
    ' LOADING HYSYS TUTORIAL FILE
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        fileName = Worksheets("Sheet1").Range("c2")
        
        If fileName <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(fileName, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If





    End If
     
    Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")
    Worksheets("Sheet1").Range("D6").Value = FEED1.MassFlow.GetValue("LB/HR")       ' import data MASS FLOWRATE from hysys streams
    Worksheets("Sheet1").Range("D7").Value = FEED1.Pressure.GetValue("PSIG")       ' import data PRESSURE from hysys streams
    Worksheets("Sheet1").Range("D8").Value = FEED1.Temperature.GetValue("C")       ' import data TEMPERATURE from hysys streams
    
    
    Set FEED2 = simCase.Flowsheet.MaterialStreams.Item("FEED2")
    
    PRESSFEED2 = Worksheets("Sheet1").Range("H7").Value
    FEED2.Pressure.SetValue PRESSFEED2, "PSIA"         ' export pressure
    
    FLOWFEED2 = Worksheets("Sheet1").Range("H6").Value
    FEED2.MolarFlow.SetValue FLOWFEED2, "MMSCFD"         ' export molar flowrate
    
    TEMPFEED2 = Worksheets("Sheet1").Range("H8").Value
    FEED2.Temperature.SetValue TEMPFEED2, "F"          ' export temperature
End Sub



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hysys Stream Parameters and units can be found here. REMEMBER TO TYPE UNIT AND PARAMETER AS DESCRIBED HERE. ,

26 comments:

  1. hi.this is a very nice blog to guide a newbie in hysys-excel automation. thanks for sharing. anw, I have a question here. Can you do the hysys-excel automation without opening the hysys file?? I want to do this since it'll be more simple and save our cpu workspace. I've tried it but got some problems in the "ActiveDocument" syntax. is there any solution to do this?? thanks.

    ReplyDelete
  2. Hi budibadibu, edit this code

    "Set hyApp = CreateObject("HYSYS.Application") hyApp.Visible = True"
    by this one
    "Set hyApp = CreateObject("HYSYS.Application") hyApp.Visible = FALSE"

    Basically, your hysys file is remainingly active, but invisible. It may save you cpu workspace. But I prefer to let it visible, so I'll know if my simulation is getting divergen.

    Hope that's help.

    ReplyDelete
  3. thanks for your answer =D. But i still get trouble with it. The message box will pop up and tell :
    run-time error'91: Object variable or With block variable not set

    here is all of my syntax for this sub, hope you can help me =D thank you very much.

    Public hycase As SimulationCase
    Public hyApp As HYSYS.Application
    Public hyOps As Operations
    Public hyFeed1, hyFeed2, hyFeed3, hyFeed4
    Public hyFS
    Public hyMix As MixerOp
    Public hysysPath As String
    Public hyPipes As PipeSegment


    Public Sub HYSYSAuto()

    'How to make connection from HYSYS and EXCEL
    hysysPath = Sheets("coba").Range("a1")
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = False
    'hyApp.SimulationCases.Open _
    hysysPath
    Set hycase = hyApp.ActiveDocument
    Set hyFS = hycase.Flowsheet (the debugger stop here)
    Set hyOps = hyFS.Operations
    'Assigning variable to material streams
    Set hyFeed1 = hyFS.MaterialStreams("DRJ-01")
    Set hyFeed2 = hyFS.MaterialStreams("DRJ-02")
    Set hyFeed3 = hyFS.MaterialStreams("DRJ-03")
    Set hyFeed4 = hyFS.MaterialStreams("DRJ-04")
    Set hyMix = hyOps.Item("MIX-100")
    MsgBox "Connection made."

    End Sub

    ReplyDelete
  4. thanks for the answer =D. but i stil get trouble with it. The message box will pop up like this :
    Run-time error'91':Object variable or With block variable not set
    Below is all of my syntax for this sub. hope u can help me =D. Thank you very much for your help.

    Public hycase As SimulationCase
    Public hyApp As HYSYS.Application
    Public hyOps As Operations
    Public hyFeed1, hyFeed2, hyFeed3, hyFeed4
    Public hyFS
    Public hyMix As MixerOp
    Public hysysPath As String
    Public hyPipes As PipeSegment


    Public Sub HYSYSAuto()

    'How to make connection from HYSYS and EXCEL
    hysysPath = Sheets("coba").Range("a1")
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = False
    'hyApp.SimulationCases.Open _
    hysysPath
    Set hycase = hyApp.ActiveDocument
    Set hyFS = hycase.Flowsheet (* the debugger will stop here)
    Set hyOps = hyFS.Operations
    'Assigning variable to material streams
    Set hyFeed1 = hyFS.MaterialStreams("DRJ-01")
    Set hyFeed2 = hyFS.MaterialStreams("DRJ-02")
    Set hyFeed3 = hyFS.MaterialStreams("DRJ-03")
    Set hyFeed4 = hyFS.MaterialStreams("DRJ-04")
    Set hyMix = hyOps.Item("MIX-100")
    MsgBox "Connection made."

    End Sub

    ReplyDelete
  5. hey friends.he2.actually i've solved it by looking your syntax in your post. I didn't look it carefully. Thanks for your help. =D.

    ReplyDelete
  6. It seems your code is not complete yet. hyFeed1 (and other streams) is not set correctly. Your streams declaration is not complete.

    Remove "Public hyFeed1, hyFeed2, hyFeed3, hyFeed4", try this

    "Public hyFeed1 as ProcessStream, hyFeed2 as ProcessStream, hyFeed3 as ProcessStream, hyFeed4 as ProcessStream"

    (all paramater/streams you can type in single line, but you must declare by each stream/parameter you have.

    For example, by typing this code

    Public hyFeed1, hyFeed2, hyFeed3, hyFeed4 as ProcessStream

    only hyFeed4 is declared as processstream, but others are not declared as processstream (hyFeed1, hyFeed2 and hyFeed3)


    Your material streams code is not working, try this one

    Set hyFeed1 = hycase.Flowsheet.MaterialStreams.Item("DRJ-01")
    Set hyFeed2 = hycase.Flowsheet.MaterialStreams.Item("DRJ-02")
    Set hyFeed3 = hycase.Flowsheet.MaterialStreams.Item("DRJ-03")
    Set hyFeed4 = hycase.Flowsheet.MaterialStreams.Item("DRJ-04")

    ReplyDelete
  7. haha... you didn't read my post carefully...

    ReplyDelete
  8. hei buddy. I think i need your help again. hehe. do you know how to call the length of pipe segment from hysys ?? I am struggling with that now. Thanks a lot.

    ReplyDelete
  9. um..never tried before. but i think i'm gonna try that.

    btw, if you want to share your script in my blog, just let me know, i'll post your script here.

    ReplyDelete
  10. haha. Let me know if u have succeed trying that things [=D]
    Oh, that sounds interesting. any email address that I can contact you??

    ReplyDelete
  11. send me an email to akrsuryawan@yahoo.com

    alex

    ReplyDelete
  12. Hey Alex, did you ever figure out how to call the length of a pipe segment or how to assign a cell (in excel) to set the eq-length value (in hysys)?

    Thank you for showing everything that you have!!!!!!

    ReplyDelete
  13. Nice comment. I'll show you how to access pipe segment in hysys.

    ReplyDelete
  14. Hello,
    I found your blog very usefull. But I got a problem. How to add a component to component list. I know how to create component list

    Dim EXHAUST_GAS As ComponentList
    Set EXHAUST_GAS = simCase.BasisManager.FluidPackages.Add("Component_list_1", "Pure")

    works for me, but I don't know how to add components to this list.

    ReplyDelete
  15. Hi Wojciech Skrobek,

    Thank for your question and sorry for very late response. I'll make a post to answer your question.

    ReplyDelete
  16. Hi, thank you very much for the tutorial. it is very helpful.
    I would like to ask about how can i see the list of variable in the hysys to be used on visual basic?
    e.g.
    i want to extract liquid,gas,and water volume flow from hysys to excel.
    i've tried
    Worksheets("Sheet1").Range("D6").Value = FEED1.ActualGasFlow.GetValue("m3/h")
    but it does not work

    ReplyDelete
  17. Thank you for a great tutorial! Maybe my question would suit better on you other post, "HYSYS TRAINING TO LINK HYSYS VALVE TO EXCEL", but comments seems to be disabled there. Do you know if it's possible to set Valve Opening from Excel and if yes which variable name to use? E.g. VALVE1.xx.SetValue OPENVALVE, "%", what is xx? Thanks!

    ReplyDelete
  18. Thanks so much, the codes are absolutely helpful for a beginner like me. I am now trying to call for a stream number specified in a cell in Excel to be looked for in HYSYS, instead of directly specifying a particular HYSYS stream in VBA to call for the parameters.
    How can I write the codes to this? This will be very helpful in instances where I happen to import parameters from a range of HYSYS streams. It also will allow me to specifically call for the parameters of a group of selected HYSYS streams listed in Excel.

    ReplyDelete
  19. thank you! it is very useful. Some one help me to write data to oil manager.
    G.S

    ReplyDelete
  20. Hello Sir,
    Your blog is very helpful.
    I need your help for linking excel to hysys. In my case, I am using a heater and I have to vary the heat load with time as well as the inlet and outlet pressure, temperature. I have 40 readings of heat load, pressure and temperature at different time step like 3 sec, 8 sec, 15 seconds and so on... can we simulate this type of problem in aspen hysys 7.1?

    ReplyDelete
  21. Your post on excel advanced training is really useful. I agree with your thoughts, knowledge on excel is mandatory for everyone. Excel Training in Chennai | Advanced Excel Training in Chennai

    ReplyDelete
  22. Anyone one knows how to export from excel to hysys controllers and digital points via vba?

    ReplyDelete
  23. Thanks so much for this information. I have to let you know I concur on several of the points you make here and others may require some further review, but I can see your viewpoint.
    excel vba courses london

    ReplyDelete

  24. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete