Hi,
Thank you for the videos and the great lessons. I Just finished the assignment and was looking through your code. It seems this program adds an additional cost of $20 to orders over 100′ rather than giving a discount.
Also why add intCost (which has a value of zero at this point) to intSegments? As in this line:
intCost = intCost + intSegments * 40 ? Is there some subtlety in vba why this is a good idea?
If you input a number less than 20′ it returns that 2 segments are required.
May I suggest this code:
Option Explicit
Dim strMaterial As String
Dim intCost As Integer
Dim intTubeLength As Integer
Dim intSegments As Integer
Sub main()
intCost = 0
intSegments = 0
‘User defined values
intTubeLength = 99
strMaterial = “Copper”
‘Check if the tube length is positive
If intTubeLength <= 0 Then
MsgBox "Not a valid length"
Exit Sub
End If
Select Case strMaterial
Case "PVC"
intCost = 20
Case "Copper"
intCost = 50
Case "Stainless"
intCost = 60
Case "Titanium"
intCost = 100
Case Else
MsgBox "Type of material not recognized"
Exit Sub
End Select
intSegments = Round(intTubeLength \ 20 + 1, 0)
If intTubeLength < 100 Then 'Extra charge for less than 100'
MsgBox "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost + 20
Else
MsgBox "There is a discount for orders over 100'" _
& vbNewLine & "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost
End If
‘user defined values
intTubeLength = 1000
strMaterial = “Titanium”
‘determine number of segments
intSegments = intTubeLength \ 20
If intSegments < 1 And intTubeLength 0 Then
intSegments = 1
End If
If intTubeLength Mod 20 0 Then intSegments = intSegments + 1
‘find cost based on material and number of segments
Select Case strMaterial
Case “PVC”
intCost = intCost + intSegments * 40
Case “Copper”
intCost = intCost + intSegments * 50
Case “Stainless steel”
intCost = intCost + intSegments * 60
Case “Titanium”
intCost = intCost + intSegments * 100
Case Else
MsgBox “Material not recognized.”
Exit Sub
End Select
‘discount applies to orders of over 100 feet
If intTubeLength > 100 Then
intCost = intCost – (10 * intSegments)
blnDiscount = True
End If
‘display results
MsgBox “The total cost is: $” & intCost & vbCrLf & “Segments required: ” _
& intSegments & vbCrLf & “Discount applied: ” & blnDiscount
End Sub
‘Check If Tube Length is positive
If intTubelength <= 0 Then
MsgBox "Tube Length is not Valid"
Exit Sub
End If
'Selecting Case
Select Case strMaterial
Case "PVC"
intCost = 20
Case "Copper"
intCost = 50
Case "Stainless"
intCost = 60
Case "Titanium"
intCost = 100
Case Else
MsgBox "Type of Material Not Recoginized"
Exit Sub
End Select
' determining Number of segemts
intSegments = Round(intTubelength / 20, 0)
If intTubelength Mod 20 0 Then intSegments = intSegments + 1
‘For setting Up for Discount
If intTubelength > 100 Then
blnDiscount = True
End If
‘Final Message Box
If intTubelength < 100 Then 'Extra charges For less Than 100'
MsgBox "Number of Segments is:" & intSegments & vbCrLf & " Total Cost = $" & intSegments * intCost + 20
Else
MsgBox "There is a Discount for order over 100" & vbCrLf & "Number of Segments is:" & intSegments & vbCrLf & " Total Cost = $" & intSegments * intCost & vbCrLf & " Discount Applied : " & blnDiscount
Ready to start learning the SolidWorks API? Sign up for FREE membership here.Keep up with new videos, macros, and training events by joining our mailing list:
Sir,
I have some questions regarding this program:
1) What is the purpose of this line?
If intTubeLength Mod 20 0 Then intSegments = intSegments + 1
2) Cost = No. of Segments * Cost/Segment, then why you use “intCost = intCost + intSegments * 40″.
3) When I use intTubelength = 19. It gives me “Segment Required = 2″
It must be 1 according to Statement of question.
Hi,
Thank you for the videos and the great lessons. I Just finished the assignment and was looking through your code. It seems this program adds an additional cost of $20 to orders over 100′ rather than giving a discount.
Also why add intCost (which has a value of zero at this point) to intSegments? As in this line:
intCost = intCost + intSegments * 40 ? Is there some subtlety in vba why this is a good idea?
If you input a number less than 20′ it returns that 2 segments are required.
May I suggest this code:
Option Explicit
Dim strMaterial As String
Dim intCost As Integer
Dim intTubeLength As Integer
Dim intSegments As Integer
Sub main()
intCost = 0
intSegments = 0
‘User defined values
intTubeLength = 99
strMaterial = “Copper”
‘Check if the tube length is positive
If intTubeLength <= 0 Then
MsgBox "Not a valid length"
Exit Sub
End If
Select Case strMaterial
Case "PVC"
intCost = 20
Case "Copper"
intCost = 50
Case "Stainless"
intCost = 60
Case "Titanium"
intCost = 100
Case Else
MsgBox "Type of material not recognized"
Exit Sub
End Select
intSegments = Round(intTubeLength \ 20 + 1, 0)
If intTubeLength < 100 Then 'Extra charge for less than 100'
MsgBox "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost + 20
Else
MsgBox "There is a discount for orders over 100'" _
& vbNewLine & "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost
End If
End Sub
Thanks Adam, these fixes will be implemented when the course is updated.
or you could do something like this and give them a discount for each segment you will need
Option Explicit
Dim intCost As Integer
Dim intTubeLength As Integer
Dim intSegments As Integer
Dim strMaterial As String
Dim blnDiscount As Boolean
Sub main()
‘set initial values
intCost = 0
intSegments = 0
blnDiscount = False
‘user defined values
intTubeLength = 1000
strMaterial = “Titanium”
‘determine number of segments
intSegments = intTubeLength \ 20
If intSegments < 1 And intTubeLength 0 Then
intSegments = 1
End If
If intTubeLength Mod 20 0 Then intSegments = intSegments + 1
‘find cost based on material and number of segments
Select Case strMaterial
Case “PVC”
intCost = intCost + intSegments * 40
Case “Copper”
intCost = intCost + intSegments * 50
Case “Stainless steel”
intCost = intCost + intSegments * 60
Case “Titanium”
intCost = intCost + intSegments * 100
Case Else
MsgBox “Material not recognized.”
Exit Sub
End Select
‘discount applies to orders of over 100 feet
If intTubeLength > 100 Then
intCost = intCost – (10 * intSegments)
blnDiscount = True
End If
‘display results
MsgBox “The total cost is: $” & intCost & vbCrLf & “Segments required: ” _
& intSegments & vbCrLf & “Discount applied: ” & blnDiscount
End Sub
Dear All, Mod has been added to avoid Segments Round off
Option Explicit
Dim intSegments As Integer
Dim intTubelength As Integer
Dim strMaterial As String
Dim intCost As Integer
Dim blnDiscount As Boolean
Sub main()
‘Set Intial Values
intCost = 0
intSegments = 0
blnDiscount = False
‘User Inputs
intTubelength = 66
strMaterial = “Copper”
‘Check If Tube Length is positive
If intTubelength <= 0 Then
MsgBox "Tube Length is not Valid"
Exit Sub
End If
'Selecting Case
Select Case strMaterial
Case "PVC"
intCost = 20
Case "Copper"
intCost = 50
Case "Stainless"
intCost = 60
Case "Titanium"
intCost = 100
Case Else
MsgBox "Type of Material Not Recoginized"
Exit Sub
End Select
' determining Number of segemts
intSegments = Round(intTubelength / 20, 0)
If intTubelength Mod 20 0 Then intSegments = intSegments + 1
‘For setting Up for Discount
If intTubelength > 100 Then
blnDiscount = True
End If
‘Final Message Box
If intTubelength < 100 Then 'Extra charges For less Than 100'
MsgBox "Number of Segments is:" & intSegments & vbCrLf & " Total Cost = $" & intSegments * intCost + 20
Else
MsgBox "There is a Discount for order over 100" & vbCrLf & "Number of Segments is:" & intSegments & vbCrLf & " Total Cost = $" & intSegments * intCost & vbCrLf & " Discount Applied : " & blnDiscount
End If
End Sub
Really appreciate the tutorials.
Dim intNumsegments As Integer
Dim intCost As Integer
Dim strPipematerial As String
Dim intReqLength As Integer
Sub main()
strPipematerial = “Stainless Steel”
intReqLength = 105
intNumsegments = intReqLength \ 20
If intReqLength <= 20 Then
intNumsegments = 1
ElseIf intReqLength Mod 20 0 Then
intNumsegments = intNumsegments + 1
End If
Select Case strPipematerial
Case “PVC”
intCost = 40 * intNumsegments
Case “Copper”
intCost = 50 * intNumsegments
Case “Stainless Steel”
intCost = 60 * intNumsegments
Case “Titanium”
intCost = 100 * intNumsegments
End Select
If intReqLength < 100 Then
intCost = intCost + 20
End If
MsgBox "Material: " & strPipematerial & vbCrLf & "Cost :" & intCost
End Sub