Running Vensim from Excel

Use this forum to post Vensim related questions.
Post Reply
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Running Vensim from Excel

Post by olive »

Good evening,
I have a very simple model that I'm trying to run from Excel. I can get the values of the variables from Vensim to Excel without any problem but for some reason I cannot pass a new value to a Vensim variable from Excel. The variable is defined in Vensim as a Gaming variable. Here is the code I use.

Code: Select all

Option Explicit

Private Declare Function vensim_be_quiet Lib "vendll32.dll" (ByVal quietflag As Long) As Long
Private Declare Function vensim_check_status Lib "vendll32.dll" () As Long
Private Declare Function vensim_command Lib "vendll32.dll" (ByVal Vcommand$) As Long
Private Declare Function vensim_continue_simulation Lib "vendll32.dll" (ByVal number_time_step As Long) As Long
Private Declare Function vensim_finish_simulation Lib "vendll32.dll" () As Long
Private Declare Function vensim_get_data Lib "vendll32.dll" (ByVal filename$, ByVal varname$, ByVal timename$, varvals As Single, timevals As Single, ByVal maxpoints As Integer) As Long
Private Declare Function vensim_get_dpval Lib "vendll32.dll" (ByVal varname$, varval As Double) As Long
Private Declare Function vensim_get_dpvecvals Lib "vendll32.dll" (vecoff As Long, varvals As Double, ByVal veclen As Long) As Long
Private Declare Function vensim_get_info Lib "vendll32.dll" (ByVal infowanted As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_sens_at_time Lib "vendll32.dll" (ByVal filename$, ByVal varname$, ByVal timename$, attime As Single, vals As Single, ByVal maxpoint As Long) As Long
Private Declare Function vensim_get_substring Lib "vendll32.dll" (ByVal fullstring$, ByVal frompos As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_val Lib "vendll32.dll" (ByVal varname$, varval As Single) As Integer
Private Declare Function vensim_get_varattrib Lib "vendll32.dll" (ByVal varname$, ByVal attrib As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_varnames Lib "vendll32.dll" (ByVal filter$, ByVal vartype As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_varoff Lib "vendll32.dll" (ByVal varname$) As Long
Private Declare Function vensim_get_vecvals Lib "vendll32.dll" (vecoff As Long, varvals As Single, ByVal nelm As Long) As Long
Private Declare Function vensim_set_parent_window Lib "vendll32.dll" (ByVal vwidnow As Long, ByVal r1 As Long, ByVal r2 As Long) As Long
Private Declare Function vensim_show_sketch Lib "vendll32.dll" (ByVal viewnum As Long, ByVal wantscroll As Long, ByVal zoompercent As Long, ByVal Vwindow As Long) As Long
Private Declare Function vensim_start_simulation Lib "vendll32.dll" (ByVal loadfirst As Integer, ByVal game As Long, ByVal overwrite As Long) As Long
Private Declare Function vensim_tool_command Lib "vendll32.dll" (ByVal Vcommand$, ByVal Vwindow As Long, ByVal iswip As Long) As Long

Sub simulate_demand()

Dim result As Integer
Dim varstr As String
Dim rval1(10) As Single
Dim rval2(10) As Single
Dim rval3(10) As Single
Dim rval4(10) As Single
Dim rval5(10) As Single
Dim rval6(10) As Single
Dim tval(10) As Single
Dim num As Single
Dim i As Single
Dim j As Single

result = vensim_be_quiet(2)

varstr = "SPECIAL>LOADMODEL|" & ThisWorkbook.Path & "\ModelB.vpm"

result = vensim_command(varstr)
result = vensim_command("SIMULATE>CHGFILE")
result = vensim_command("SIMULATE>DATA")
result = vensim_command("SIMULATE>RUNNAME|game")
result = vensim_command("MENU>GAME")

result = vensim_command("SIMULATE>SETVAL|ludo = 3") 

MsgBox result  'This returns 1 

result = vensim_get_data("Current2.vdf", "olive", "Time", rval4(1), tval(1), 10)
For i = 1 To result
    Sheet3.Range("K3").Offset(i, 0) = tval(i)  'Time
    Sheet3.Range("L3").Offset(i, 0) = rval4(i)  'Olive
Next i

result = vensim_get_data("Current2.vdf", "ludo", "Time", rval5(1), tval(1), 10)
For i = 1 To result
    Sheet3.Range("N3").Offset(i, 0) = tval(i)  'Time
    Sheet3.Range("O3").Offset(i, 0) = rval5(i)  'ludo
Next i

result = vensim_get_data("Current2.vdf", "toto", "Time", rval6(1), tval(1), 10)
For i = 1 To result
    Sheet3.Range("Q3").Offset(i, 0) = tval(i)  'Time
    Sheet3.Range("R3").Offset(i, 0) = rval6(i)  'toto
Next i

result = vensim_command("GAME>ENDGAME")

End Sub
When I run the code the variable "ludo" keeps the initial value I gave it in the model (20), not the one I'm trying to pass from Excel (3)...
Any help on this would be greatly appreciated :)
Thank you very much for your time.
Administrator
Super Administrator
Posts: 4842
Joined: Wed Mar 05, 2003 3:10 am

Re: Running Vensim from Excel

Post by Administrator »

Two questions.

1. Is ludo a gaming variable?

2. What happens if you use

Code: Select all

result = vensim_command("MENU>GAME")
result = vensim_command("GAME>GAMEON")
result = vensim_command("SIMULATE>SETVAL|ludo = 3") 
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Running Vensim from Excel

Post by olive »

1. Yes, ludo is a gaming variable

2. It doesn't work :( ludo keeps the initial value I gave it in the model (20)
Administrator
Super Administrator
Posts: 4842
Joined: Wed Mar 05, 2003 3:10 am

Re: Running Vensim from Excel

Post by Administrator »

Can you upload the Excel spreadsheet and model so we can test?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Running Vensim from Excel

Post by olive »

Here it is! Thank you very much for your help!
Attachments
ConnectotModelAB.zip
(54.18 KiB) Downloaded 1231 times
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Running Vensim from Excel

Post by olive »

Hi,
Were you able to identify where the problem might coming from? I'm still unsuccessful at passing a value to a gaming variable from Excel... :/
Thank you,
Olivia
Administrator
Super Administrator
Posts: 4842
Joined: Wed Mar 05, 2003 3:10 am

Re: Running Vensim from Excel

Post by Administrator »

Sorry Olivia, I missed the post.

I've just checked and it's working fine. The problem is that you are not actually moving the game forward. Try

Code: Select all

result = vensim_command("SIMULATE>SETVAL|ludo = 3") 
result = vensim_command("GAME>GAMEON")
result = vensim_command("GAME>GAMEON")
result = vensim_command("GAME>GAMEON")
result = vensim_command("GAME>GAMEON")
result = vensim_command("GAME>GAMEON")
Tony.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Running Vensim from Excel

Post by olive »

Hi Tony,
It still doesn't work on my end... It keeps pulling the value that is in the model (20), not the one I'm trying to pass (3). Also, how do you know how many times you need to move the game forward, in other words, why do you need so many GAME>GAMEON?
Thank you very much for your help,
Olivia
Administrator
Super Administrator
Posts: 4842
Joined: Wed Mar 05, 2003 3:10 am

Re: Running Vensim from Excel

Post by Administrator »

The other error in your code is that you are displaying the results from current.vdf instead of game.vdf.

Try the attached file.
Attachments
ConnectoModelAB.xlsm
(22.58 KiB) Downloaded 1132 times
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
olive
Member
Posts: 22
Joined: Tue Sep 13, 2011 8:30 pm

Re: Running Vensim from Excel

Post by olive »

That makes sense. Thank you very much for your help!
Post Reply