Hi All,
Currently i am trying to convert an old MS BASIC code into VBA for use with an Excel Spreadsheet. The MS BASIC Code is as per below.
As you can see at Line 370 the For J = 1 to NS is initiated and completes it loop at like 430. However at line 480 it seems to jump back into the loop. Is there a way i can achieve this in VBA?
Currently my VBA code (apologies for the untidiness, i haven't cleaned it up) is as follows.
This however doesn't produce the same results when compared with the BASIC program using the following inputs
Segment 1 Length 68 Slope 0.13 Roughness 0.04
Segment 2 Length 30 Slope 0.01 Roughness 0.035
Segment 3 Length 15 Slope 0.11 Roughness 0.04
Appreciate any direction on this one
Currently i am trying to convert an old MS BASIC code into VBA for use with an Excel Spreadsheet. The MS BASIC Code is as per below.
Code:
10 REM TIME OF CONCENTRATION PROGRAM FOR OVERLAND FLOWS
20 REM USING KINEMATIC WAVE EQUATION
30 REM (IN MICROSOFT BASIC)
40 REM
50 DIM L(20),N(20),S(20)
60 REM CONSTANTS FOR 2 YEAR ARI INTENSITIES AT PENRITH, NSW
70 A = 3.382
80 B = -.579
90 C = -.0208
100 D = .00914
110 E = -.001066
120 F = -.0004188
130 G = .0000614
140 REM
150 PRINT
160 PRINT "TIME OF CONCENTRATION FOR OVERLAND FLOWS"
170 PRINT
180 INPUT "PROPERTY DRAINAGE + GUTTER FLOW TIME (MIN)?", CTIME
190 PRINT
200 PRINT "NUMBER OF OVERLAND FLOW SEGMENTS?"
210 INPUT NS
220 FOR J = 1 TO NS
230 PRINT "LENGTH (m), SLOPE (m/m) AND ROUGHNESS FOR SEGMENT" ;J
240 INPUT L(J),S(J),N(J)
250 NEXT J
260 TI = 5
270 CC = 1
280 PRINT "ITERATION INTENSITY ESTIMATED AND CALCULATED TIMES"
290 PRINT" NO. (mm/h) (min) (min)"
300 PRINT
310 REM START OF ITERATIONS
320 T = CTIME
330 TL = 0
340 REM CALCULATE INTENSITY FOR GIVEM TIME 'TI'
350 LT = LOG(TI / 60)
360 I = EXP(A + B*LT + C*LT^2 + D*LT^3 + E*LT^4 + F*LT^5 + G*LT^6)
370 FOR J = 1 TO NS
380 FL = TL
390 TL = TL + L(J)
400 T = T + 6.94 * (TL*N(J))^.6 / (I^.4 * S(J)^.3)
410 IF J = 1 THEN GOTO 430
420 T = T - 6.94 * (FL*N(J))^.6 / (I^.4 * S(J)^.3)
430 NEXT J
440 PRINT USING " ###, ####.#, ####.##, ####.##";CC,I,TI,T
450 IF ABS(T-TI) < 0.01 THEN GOTO 490
460 CC = CC + 1
470 TI = T
480 GOTO 320
490 PRINT
500 IF T > 5 THEN GOTO 520
510 PRINT "TIME IS LESS THAN 5 minutes - VALUE BELOW IS NOT EXACT"
520 PRINT USING "TIME IS ####.# minutes FOR INTENSITY ####.# mm/h";T,I
530 END
As you can see at Line 370 the For J = 1 to NS is initiated and completes it loop at like 430. However at line 480 it seems to jump back into the loop. Is there a way i can achieve this in VBA?
Currently my VBA code (apologies for the untidiness, i haven't cleaned it up) is as follows.
Code:
Private Sub CommandButton2_Click()
Dim ctime As String, NS As Double, tbl As ListObject, FSTable As Range, L As Range, Results As Range, R As Double, T As Double, S As Double
Set tbl = ActiveSheet.ListObjects("FS")
Set Res = ActiveSheet.ListObjects("Table2")
Set FSTable = ActiveSheet.Range("FS")
NS = tbl.Range.Rows.Count - 1
A = Range("CoefA").Value
B = Range("CoefB").Value
C = Range("CoefC").Value
D = Range("CoefD").Value
E = Range("CoefE").Value
F = Range("CoefF").Value
G = Range("CoefG").Value
TI = 5
CC = 1
ctime = Range("Ctime").Value
TL = 0
T = 0
Iteration = 1
For Each L In FSTable.Columns(2).Cells
If Iteration > 1 Then
T = ctime
TL = 0
Else
End If
LT = Log((TI / 60))
INTENSITY = Exp(A + (B * LT) + (C * LT ^ 2) + (D * LT ^ 3) + (E * LT ^ 4) + (F * LT ^ 5) + (G * LT ^ 6))
S = FSTable.Cells(Iteration, 3)
R = FSTable.Cells(Iteration, 4)
FL = TL
TL = TL + L
T = T + (6.94 * (((TL * R) ^ 0.6) / ((INTENSITY ^ 0.4) * (S ^ 0.3))))
If Iteration = 1 Then
GoTo Skip
Else
T = T - (6.94 * (((FL * R) ^ 0.6) / ((INTENSITY ^ 0.4) * (S ^ 0.3))))
End If
If Abs(T - TI) < 0.01 Then
GoTo Display
Else
End If
Display:
Res.DataBodyRange(Iteration, 3).Value = TI
Skip:
Res.DataBodyRange(Iteration, 1).Value = Iteration
Res.DataBodyRange(Iteration, 2).Value = INTENSITY
Res.DataBodyRange(Iteration, 3).Value = TI
Res.DataBodyRange(Iteration, 4).Value = T
TI = T
Iteration = Iteration + 1
Next
End Sub
This however doesn't produce the same results when compared with the BASIC program using the following inputs
Segment 1 Length 68 Slope 0.13 Roughness 0.04
Segment 2 Length 30 Slope 0.01 Roughness 0.035
Segment 3 Length 15 Slope 0.11 Roughness 0.04
Appreciate any direction on this one