I am new to programming in VB so my knowledge and fault finding is a little basic! I need to model the voltage reactions of a neuron and so far I have this piece of code:

Private Sub CmdRun_Click()
'Declare all variables as doubles to allow floating point calculations
Dim dVoltage As Double '1
Dim dAlphaH As Double '2
Dim dBetaH As Double '3
Dim dAlphaM As Double '4
Dim dBetaM As Double '5
Dim dAlphaN As Double '6
Dim dBetaN As Double '7
Dim dGK As Double '8
Dim dGNa As Double '9
Dim dGLeak As Double '10
Dim dVK As Double '11
Dim dVNa As Double '12
Dim dVLeak As Double '13
Dim dH As Double '14
Dim dM As Double '15
Dim dN As Double '16
Dim dIK As Double '17
Dim dINa As Double '18
Dim dILeak As Double '19
Dim dIMembrane As Double '20
Dim dTimeElapsed As Double '21
Dim dTimeInterval As Double '22


Dim dTotalTime As Double 'Total time for the simulation
Dim dY As Double 'Number of steps as a double
Dim iLoopMax As Integer 'Number of steps as an integer
Dim iLoopCount As Integer 'Loop count variable



dTotalTime = txtTotaltime

dTimeInterval = txtTimeInterval


'Declare the arrays needed
Dim VoltageArray() As Double
Dim VoltageResults() As Double

dY = dTotalTime / dTimeInterval
iLoopMax = Int(dY)

ReDim VoltageArray(1 To 22, 1 To iLoopMax)
ReDim VoltageResults(1 To iLoopMax, 1 To 2)

iLoopCount = 1
dTimeElapsed = 0
'Set the loop which will fill the array
Do Until iLoopCount = iLoopMax


If dTimeElapsed >= txtBeginClamp And dTimeElapsed <= txtEndClamp Then
dVoltage = txtVClamp
ElseIf dTimeElapsed < txtBeginClamp Or dTimeElapsed > txtEndClamp Then
dVoltage = txtRestP
End If


VoltageArray(1, iLoopCount) = dVoltage


dAlphaH = 0.07 * Exp(-(VoltageArray(1, iLoopCount) + 65) / 20)
dBetaH = 1 / (1 + Exp(-(VoltageArray(1, iLoopCount) + 35) / 10))
dAlphaM = (0.1 * (VoltageArray(1, iLoopCount) + 40)) / (1 - Exp(-(VoltageArray(1, iLoopCount) + 40) / 10))
dBetaM = 4 * Exp(-(VoltageArray(1, iLoopCount) + 65) / 18)
dAlphaN = (0.01 * (VoltageArray(1, iLoopCount) + 55)) / (1 - Exp((-(VoltageArray(1, iLoopCount) + 55)) / 10))
dBetaN = 0.125 * Exp(-(VoltageArray(1, iLoopCount) + 65) / 80)


VoltageArray(2, iLoopCount) = dAlphaH
VoltageArray(3, iLoopCount) = aBetaH
VoltageArray(4, iLoopCount) = dAlphaM
VoltageArray(5, iLoopCount) = dBetaM
VoltageArray(6, iLoopCount) = dAlphaN
VoltageArray(7, iLoopCount) = dBetaN

dGK = txtGK
dGNa = txtGNa
dGLeak = txtGLeak
dVK = txtVK
dVNa = txtVNa
dVLeak = txtVLeak


VoltageArray(8, iLoopCount) = dGK
VoltageArray(9, iLoopCount) = dGNa
VoltageArray(10, iLoopCount) = dGLeak
VoltageArray(11, iLoopCount) = dVK
VoltageArray(12, iLoopCount) = dVNa
VoltageArray(13, iLoopCount) = dVLeak

If iLoopCount = 1 Then

dH = VoltageArray(2, 1) / (VoltageArray(2, 1) + VoltageArray(3, 1))
dM = VoltageArray(4, 1) / (VoltageArray(4, 1) + VoltageArray(5, 1))
dN = VoltageArray(6, 1) / (VoltageArray(6, 1) + VoltageArray(7, 1))

VoltageArray(14, 1) = dH
VoltageArray(15, 1) = dM
VoltageArray(16, 1) = dN

dIK = ((VoltageArray(16, 1) ^ 4) * VoltageArray(8, 1) * (VoltageArray(1, 1) - VoltageArray(11, 1)))
dINa = ((VoltageArray(15, 1) ^ 3) * VoltageArray(14, 1) * VoltageArray(9, 1) * (VoltageArray(1, 1) - VoltageArray(12, 1)))
dILeak = (VoltageArray(10, 1) * (VoltageArray(1, 1) - VoltageArray(13, 1)))

VoltageArray(17, 1) = dIK
VoltageArray(18, 1) = dINa
VoltageArray(19, 1) = dILeak

dIMembrane = VoltageArray(17, 1) + VoltageArray(18, 1) + VoltageArray(19, 1)
VoltageArray(20, 1) = dIMembrane


VoltageArray(21, 1) = 0

VoltageResults(1, 1) = dTimeElapsed
VoltageResults(1, 2) = dIMembrane


Else
VoltageArray(22, iLoopCount) = txtTimeInterval
VoltageArray(21, iLoopCount) = VoltageArray(21, (iLoopCount - 1)) + VoltageArray(22, iLoopCount)

dH = VoltageArray(14, (iLoopCount - 1)) + (((VoltageArray(2, iLoopCount) * (1 - VoltageArray(14, (iLoopCount - 1))) - VoltageArray(3, iLoopCount) * VoltageArray(14, (iLoopCount - 1))))) * VoltageArray(22, iLoopCount)
dM = VoltageArray(15, (iLoopCount - 1)) + (((VoltageArray(4, iLoopCount) * (1 - VoltageArray(15, (iLoopCount - 1))) - VoltageArray(5, iLoopCount) * VoltageArray(15, (iLoopCount - 1))))) * VoltageArray(22, iLoopCount)
dN = VoltageArray(16, (iLoopCount - 1)) + (((VoltageArray(6, iLoopCount) * (1 - VoltageArray(16, (iLoopCount - 1))) - VoltageArray(7, iLoopCount) * VoltageArray(16, (iLoopCount - 1))))) * VoltageArray(22, iLoopCount)

VoltageArray(14, iLoopCount) = dH
VoltageArray(15, iLoopCount) = dM
VoltageArray(16, iLoopCount) = dN

dIK = ((VoltageArray(16, iLoopCount) ^ 4) * VoltageArray(8, iLoopCount) * (VoltageArray(1, iLoopCount) - VoltageArray(11, iLoopCount)))
dINa = ((VoltageArray(15, iLoopCount) ^ 3) * VoltageArray(14, iLoopCount) * VoltageArray(9, iLoopCount) * (VoltageArray(1, iLoopCount) - VoltageArray(12, iLoopCount)))
dILeak = (VoltageArray(10, iLoopCount) * (VoltageArray(1, iLoopCount) - VoltageArray(13, iLoopCount)))

VoltageArray(17, iLoopCount) = dIK
VoltageArray(18, iLoopCount) = dINa
VoltageArray(19, iLoopCount) = dILeak

dIMembrane = VoltageArray(17, iLoopCount) + VoltageArray(18, iLoopCount) + VoltageArray(19, iLoopCount)
VoltageArray(20, iLoopCount) = dIMembrane

VoltageResults(iLoopCount, 1) = dTimeElapsed
VoltageResults(iLoopCount, 2) = dIMembrane


End If

iLoopCount = iLoopCount + 1

MSChart1.ChartData = VoltageResults

Loop

End Sub

Unfortunately it does not seem to store the elapsed time properly. In time period 1 I need a time value of zero that increments with each iloopcount by the value of timeinterval. this time elapsed value needs to be compared to the begin and endclamp values to see if the voltage value should be changed from txtrestp to txtvclamp when the clamp is working. Sorry if this seems very confusing - it does to me at the minute too!
Alternatively does anybody know how I can output the voltagearray to excel or a flexgrid (I cannot get flexgrid to accept an array as a datasource in the same way as i can assign an array to MSChart)
initial values for txtgk = 36, txtgna = 120, txtgleak = 0.3,
txtvna = 5, txtvk =-77, txtvleak =-54.4 ,txttimeinterval = 0.05, txttotaltime = 60, txtvclamp = 10, txtrestp = -70, txtbeginclamp = 10, txtendclamp = 40. (the simulation should run for 60 seconds with an observation every 0.05 seconds.dvoltage should change after 10 seconds and return to its resting value after 40seconds). Sorry if this explanation is a bit over the top - I just wanted to give as much info as I have!

Thanks in advance for any help. You are a lifesaver!