Jump to content

Featured Articles

Check out the latest featured articles.

File Library

Check out the latest downloads available in the File Library.

New Article

Product Viscosity vs. Shear

Featured File

Vertical Tank Selection

New Blog Entry

Low Flow in Pipes- posted in Ankur's blog


Automation Of Spreadsheet Exported Variable (Hysys-Vba)

hysys vba automation spreadsheet exported variable

3 replies to this topic
Share this topic:
| More

#1 Thegeceb


    Brand New Member

  • Members
  • 2 posts

Posted 28 June 2022 - 09:03 AM

Hello everyone,


I'm developing a VBA-HYSYS automation for a depressurization procedure. Besides one particular component, everything ran perfectly.
I'm simulating a runaway in my reactor during depressurization and using a Spreadsheet to calculate dynamic heat that is transferred to the reactor via a heat stream.
Without automation, I manually connect the cells in the spreadsheet where the runaway heat is calculated and the heat stream with an exported variable. I'm experiencing difficulties doing the same thing in VBA.
Do you understand the correct object structure for creating an exported variable from the correct spreadsheet cell to the correct ProcessStream?
Many thanks for your response.


#2 Bobby Strain

Bobby Strain

    Gold Member

  • Members
  • 3,326 posts

Posted 28 June 2022 - 11:50 AM

You might look on the AspenTech site for help. There are lots of examples there.



#3 Thegeceb


    Brand New Member

  • Members
  • 2 posts

Posted 29 June 2022 - 02:50 AM

Thank you for responding!
I had no idea this support existed, and you are correct, there are countless examples. Here's the code I was looking for:
Sub Main()
'Description: Simple Example of adding a spreadsheet, importing a value, adding a formula
' and exporting a value
' Imports a temperature from stream 1 in the current case, and exports it to stream 2
' In Excel VBA Requires HYSYS type library to be set under Tools ... References
'Declare Variables----------------------------------------------------------------------------

Dim hyApp As HYSYS.Application 'Application
Dim hyCase As HYSYS.SimulationCase 'Case
Dim hySprd As HYSYS.SpreadsheetOp 'Spreadsheet
Dim hySprdCell As HYSYS.SpreadsheetCell 'Cell
Dim hyRV As HYSYS.RealVariable 'Real Variable


'Excel VBA - Link to HYSYS Objects - comment out to use Macro Language Editor
Set hyApp = GetObject(, "HYSYS.Application") 'Only works if HYSYS is open
Set hyCase = hyApp.ActiveDocument

'HYSYS Macro Language Editor - Link to HYSYS Objects - uncomment to use MLE
'Set hyCase = ActiveCase

'Create a new spreadsheet operation
Set hySprd = hyCase.Flowsheet.Operations.Add("MySprd", "spreadsheetop")

'Could link to an existing spreadsheet as follows
'Set hySprd = hyCase.Flowsheet.Operations.Item("MySprd")

'Import the temperature from stream 1 into cell A1

'Link to a particular cell
Set hySprdCell = hySprd.Cell("A1")

'Good practice to clear any existing imports

'Establish an object for the variable we want to import
Set hyRV = hyCase.Flowsheet.MaterialStreams.Item("1").Temperature

'Link to the cell
hySprdCell.ImportedVariable = hyRV

'Put a formula into B1
Set hySprdCell = hySprd.Cell("B1")
hySprdCell.CellText = "=A1+10"

'Now export this new value back to stream 2
'Procedure as for imports except use the .ExportedVariable property
'Note there is no need to go via the intermediate RV - can do all in one line
hySprdCell.ExportedVariable = hyCase.Flowsheet.MaterialStreams.Item("2").Temperature

End Sub

#4 Bobby Strain

Bobby Strain

    Gold Member

  • Members
  • 3,326 posts

Posted 29 June 2022 - 09:18 AM

This all looks strange to me. I use Excel for automation, not HYSYS spreadsheet. Seems it's much simpler to use Excel.



Edited by Bobby Strain, 29 June 2022 - 10:58 AM.

Similar Topics