• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Microsoft Basic Code to VBA

NicGreen

Member
Hi All,

I'm currently trying to update this Microsoft Basic code into VBA. Is it possible? I have no idea with coding however have pasted a copy of the code below. It basically is to do with gutter flow times during storm events however I like to get it to work so i can use it to save a whole lot of time. I had an attempt at converting it however im walking blindly here.

Code:
Rem Gutter and Roadway Flow Characteristics Program
Rem (In Microsoft Basics)
Debug.Print "Gutter and Roadway Flow Characteristics Program"
Debug.Print "Gutter Width (m), Half Road Width (m)?"
Input #1, GW, PW
Debug.Print "Gutter and Pavement Cross Slopes (m/m)?"
Input #2, GS, PS
Debug.Print "Gutter Face Slope (Degrees, 0 - Flat, 90 -Vertical)?"
Input #3, Theta
Debug.Print "Gutter Depth (m)?"
Input #4, GD
GS = GD / (GD / Tan(0.0174533 * Theta) + GD / GS)
If Theta = 90 Then GoTo 17
Debug.Print "Revised Gutter Cross Slope"
Debug.Print using; "Due to Sloping Face Is##.#####"; GS
Debug.Print "Gutter and PAvement Roughness?"
Input #5, GN, PN
Debug.Print "Flowrate Adjustment Factor?"
Input #6, F
Debug.Print "Longitudinal Slope (m/m)?"
Input #7, LS
Debug.Print "Gutter Length (m)?"
Input #8, Length
Rem - The Gutter Capacity is Calculated for the given depth
PD = GD - GW * GS
If PD < 0 Then PD = 0
CD = PD - (PW - GW) * PS
If CD < 0 Then CD = 0
X = 8 / 3
Q = 0.375 * F * ((GD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN)) * LS ^ 0.5
Debug.Print using; "Capacity at Given Depth is###.### m^3/s"; Q
Debug.Print
Rem - Now the user can nominate a flowrate and the program will determine flow depth, width and velocity
Debug.Print
CC = 1
Debug.Print "Trial flowrate (m^3/s)? Zero for new slope, -ve to end"
Rem Program Goes back if Zero is Entered
Input #9, Q1
If Q1 < Q Then GoTo 75
If Q1 = 0 Then GoTo 21
If Q1 > Q Then Debug.Print "Note that flow exceeds capacity"
If Q1 > Q Then Debug.Print "- Calculations ignore flow on footpaths"
TD = GD * (Q1 / Q) ^ (1 / X)
Rem Start of Iterations
PD = TD - GW * GS
CD = PD - (PW - GW) * PS
If CD < 0 Then CD = 0
If PD < 0 Then PD = 0
Q2 = 0.375 * F * ((TD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN) * LS ^ 0.5)
If Abs(Q1 - Q2) < 0.00001 Then GoTo 55
CC = CC + 1
TD = TD * (Q1 / Q2) ^ (1 / X)
GoTo 56
TW = TD / GS
If TW > GW Then TW = GW + (PD / PS)
AREA = TD * TW / 2
If TW > GW Then AREA = GW * (TD + PD) / 2 + PD * PD / PS / 2
If TW <= PW Then GoTo 62
AREA = AREA - CD * CD / PS / 2
TW = PW
Debug.Print using; "results after ## iterations :"; CC
Debug.Print using; "Flow Area is##.### m^2;AREA"
Debug.Print using; "width is###.## m"; TW
Debug.Print using; "Gutter Depth is##.### m"; TD
If CD > 0 Then Debug.Print using; " Crest Depth is ##.### m"; CD
V = Q1 / AREA
Debug.Print using; "velocity is ###.## m/s"; V
Debug.Print using; "Velocity-Depth Product is ##.##"; TD * V
Time = Length / V
TM = Time / 60
Debug.Print using; "Time is ####=(##.## minutes;)"; Time, TM
Debug.Print
GoTo 36
End
 
Hi Nic ,

Why not ?

First , you need to insert the label numbers at the appropriate places ; I have done them below , you can verify.

Second , the INPUT statements expect data ; how are you going to read in the data ? Will they be placed in the worksheet cells ?

Code:
Sub Temp()
    Rem Gutter and Roadway Flow Characteristics Program
    Rem (In Microsoft Basics)
   
    Debug.Print "Gutter and Roadway Flow Characteristics Program"
    Debug.Print "Gutter Width (m), Half Road Width (m)?"
    Input #1, GW, PW
 
    Debug.Print "Gutter and Pavement Cross Slopes (m/m)?"
    Input #2, GS, PS
 
    Debug.Print "Gutter Face Slope (Degrees, 0 - Flat, 90 -Vertical)?"
    Input #3, Theta
 
    Debug.Print "Gutter Depth (m)?"
    Input #4, GD
 
    GS = GD / (GD / Tan(0.0174533 * Theta) + GD / GS)
    If Theta = 90 Then GoTo 17
 
    Debug.Print "Revised Gutter Cross Slope"
    Debug.Print using; "Due to Sloping Face Is##.#####"; GS
17: Debug.Print "Gutter and PAvement Roughness?"
    Input #5, GN, PN
   
    Debug.Print "Flowrate Adjustment Factor?"
    Input #6, F
21: Debug.Print "Longitudinal Slope (m/m)?"
    Input #7, LS
 
    Debug.Print "Gutter Length (m)?"
    Input #8, Length
 
    Rem - The Gutter Capacity is Calculated for the given depth
    PD = GD - GW * GS
    If PD < 0 Then PD = 0
    CD = PD - (PW - GW) * PS
    If CD < 0 Then CD = 0
    X = 8 / 3
    Q = 0.375 * F * ((GD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN)) * LS ^ 0.5
    Debug.Print using; "Capacity at Given Depth is###.### m^3/s"; Q
    Debug.Print
 
    Rem - Now the user can nominate a flowrate and the program will determine flow depth, width and velocity
    Debug.Print
36: CC = 1
    Debug.Print "Trial flowrate (m^3/s)? Zero for new slope, -ve to end"
    Rem Program Goes back if Zero is Entered
    Input #9, Q1
 
    If Q1 < Q Then GoTo 75
    If Q1 = 0 Then GoTo 21
    If Q1 > Q Then Debug.Print "Note that flow exceeds capacity"
    If Q1 > Q Then Debug.Print "- Calculations ignore flow on footpaths"
    TD = GD * (Q1 / Q) ^ (1 / X)
 
    Rem Start of Iterations
    PD = TD - GW * GS
    CD = PD - (PW - GW) * PS
    If CD < 0 Then CD = 0
    If PD < 0 Then PD = 0
    Q2 = 0.375 * F * ((TD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN) * LS ^ 0.5)
    If Abs(Q1 - Q2) < 0.00001 Then GoTo 55
    CC = CC + 1
    TD = TD * (Q1 / Q2) ^ (1 / X)
    GoTo 56
55: TW = TD / GS
56: If TW > GW Then TW = GW + (PD / PS)
   
    AREA = TD * TW / 2
    If TW > GW Then AREA = GW * (TD + PD) / 2 + PD * PD / PS / 2
    If TW <= PW Then GoTo 62
    AREA = AREA - CD * CD / PS / 2
    TW = PW
62: Debug.Print using; "results after ## iterations :"; CC
    Debug.Print using; "Flow Area is##.### m^2;AREA"
    Debug.Print using; "width is###.## m"; TW
    Debug.Print using; "Gutter Depth is##.### m"; TD
    If CD > 0 Then Debug.Print using; " Crest Depth is ##.### m"; CD
    V = Q1 / AREA
    Debug.Print using; "velocity is ###.## m/s"; V
    Debug.Print using; "Velocity-Depth Product is ##.##"; TD * V
    Time = Length / V
    TM = Time / 60
    Debug.Print using; "Time is ####=(##.## minutes;)"; Time, TM
    Debug.Print
    GoTo 36
75: End
Narayan
 
Nic

It will be something like that shown below

I have commented out the non-required lines

VBA doesn't support Input # and Print Using and so I have adjuested those lines accordingly

Code:
Sub GutterFlow()

  Rem Gutter and Roadway Flow Characteristics Program
  Rem (In Microsoft Basics)
  Debug.Print "Gutter and Roadway Flow Characteristics Program"
  Debug.Print "Gutter Width (m), Half Road Width (m)?"
  GW = InputBox("Gutter width =", "Enter Gutter width")
  PW = InputBox("Pavement width =", "Enter Pavement width")
  Debug.Print "Gutter and Pavement Cross Slopes (m/m)?"
  
  GS = InputBox("Gutter slope (m/m) =", "Enter Gutter slope")
  PS = InputBox("Pavement slope (m/m) =", "Enter Pavement slope")
  Debug.Print "Gutter Face Slope (Degrees, 0 - Flat, 90 -Vertical)?"
  
  theta = InputBox("Theta =", "Enter Theta")
  
  theta = InputBox("Gutter depth =", "Gutter depth")
  Debug.Print "Gutter Depth (m)?"
  'Input #4, GD
  GS = GD / (GD / Tan(0.0174533 * theta) + GD / GS)
  If theta = 90 Then GoTo 17
  Debug.Print "Revised Gutter Cross Slope"
  Debug.Print "Due to Sloping Face Is "; GS
17: Debug.Print "Gutter and PAvement Roughness?"
  
  GN = InputBox("Gutter roughness =", "Gutter roughness")
  PN = InputBox("Gutter roughness =", "Gutter roughness")
  'Input #5, GN, PN
  
  Debug.Print "Flowrate Adjustment Factor?"
  F = InputBox("Flowrate Adjustment Factor =", "Flowrate Adjustment Factor")
  'Input #6, F

21: LS = InputBox("Longitudinal Slope =", "Longitudinal Slope")
  Debug.Print "Longitudinal Slope (m/m)?"; LS
  'Input #7, LS
  
  Length = InputBox("Gutter Length =", "Gutter Length")
  Debug.Print "Gutter Length (m)?"; Length
  'Input #8, Length
  
  Rem - The Gutter Capacity is Calculated for the given depth
  PD = GD - GW * GS
  If PD < 0 Then PD = 0
  CD = PD - (PW - GW) * PS
  If CD < 0 Then CD = 0
  X = 8 / 3
  Q = 0.375 * F * ((GD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN)) * LS ^ 0.5
  Debug.Print "Capacity at Given Depth is "; Q; " m^3/s"
  Debug.Print
  
  Rem - Now the user can nominate a flowrate and the program will determine flow depth, width and velocity
  Debug.Print
36: CC = 1
  Debug.Print "Trial flowrate (m^3/s)? Zero for new slope, -ve to end"
  
  Rem Program Goes back if Zero is Entered
  
  Q1 = InputBox("Q1 =", "Q1")
  'Input #9, Q1
  If Q1 < Q Then GoTo 75
  If Q1 = 0 Then GoTo 21
  If Q1 > Q Then Debug.Print "Note that flow exceeds capacity"
  If Q1 > Q Then Debug.Print "- Calculations ignore flow on footpaths"
  TD = GD * (Q1 / Q) ^ (1 / X)
  Rem Start of Iterations
  PD = TD - GW * GS
  CD = PD - (PW - GW) * PS
  If CD < 0 Then CD = 0
  If PD < 0 Then PD = 0
  Q2 = 0.375 * F * ((TD ^ X - PD ^ X) / (GS * GN) + (PD ^ X - CD ^ X) / (PS * PN) * LS ^ 0.5)
  If Abs(Q1 - Q2) < 0.00001 Then GoTo 55
  CC = CC + 1
  TD = TD * (Q1 / Q2) ^ (1 / X)
  GoTo 56
55: TW = TD / GS
56: If TW > GW Then TW = GW + (PD / PS)
  Area = TD * TW / 2
  If TW > GW Then Area = GW * (TD + PD) / 2 + PD * PD / PS / 2
  If TW <= PW Then GoTo 62
  Area = Area - CD * CD / PS / 2
  TW = PW
  
62: Debug.Print "results after "; CC; " iterations "
  Debug.Print "Flow Area is "; Area; "M^2"
  Debug.Print "width is "; TW; " m"
  Debug.Print "Gutter Depth is "; TD; " m"
  If CD > 0 Then Debug.Print using; " Crest Depth is ##.### m"; CD
  V = Q1 / Area
  Debug.Print "velocity is "; V; " m/s"
  Debug.Print "Velocity-Depth Product is "; TD * V
  Time = Length / V
  TM = Time / 60
  Debug.Print "Time is "; TM; " minutes"
  Debug.Print
GoTo 36

75: End

End Sub

Having said all that it would be just as simple to setup this in a worksheet using cells and formula
 
Last edited:
Hi, NicGreen!
What we can't provide you is with an amber (even I assume you'd prefer green) phosphor Hercules monitor to test it. PC XT 4.77Mhz yes, Hui's still using one, but he asked Santa for an upgrade to CGA 16 color monitor.
Regards!
 
What would you suggest would be the best input method I'm assuming the only type is through the use of a workbook cell?
 
Hi Nic ,

The InputBox function displays a dialog box for the user to enter either data or select a range in the worksheet.

Alternatively , you can have a userform , where the user enters all the data at one go , and when all data has been entered , the code can process it.

As a third option , all of the data can be manually entered in the worksheet , and the code can retrieve the data from the cells.

Narayan
 
My thoughts would be that given the nature of the issue we are addressing the input box would be the best option.
 
Hi Nic ,

In that case , you can just copy Hui's code , since he has already incorporated it. Copy + paste it in the VBE in an empty workbook , and run it and see how it does.

Narayan
 
Posting a valid set of input data would be useful for testing purposes
 
Thanks guys sorry for the late response. Been bogged down on other projects. Your comments are greatly appreciated.
 
It may be a PC XT 4.77Mhz
But I do have the 80287 co-processor, so it really hums along
 
I would still recommend that you set that up as a spreadsheet without VBA Code

It lends itself to that quite easily
 
@Hui
Hi!
If it's a 80287 processor you surely will have it on the newer PC AT 80286 6/8 Mhz,,, that will be shipping in 2015 Q4, for Xmas time.
XTs 8086 only supported 8087 floating-point coprocessors.
Regards!
 
Back
Top