'=========================================================
'==            Visual Basic Sample Code From            ==
'==     Microsoft Excel/Visual Basic for Windows 95     ==
'==                  Programmer's Guide                 ==
'==                      Chapter 1                      ==
'=========================================================

' DISCLAIMER OF WARRANTY

' THIS FILE CONTAINS UNDOCUMENTED SAMPLE CODE. THIS SAMPLE CODE IS 
' PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT FURTHER
' DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING WITHOUT LIMITATION ANY
' IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS FOR A PARTICULAR
' PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE
' SAMPLE CODE REMAINS WITH YOU.

' IN NO EVENT SHALL MICROSOFT OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES
' WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF
' BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
' OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE
' THIS SAMPLE CODE, EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY
' OF SUCH DAMAGES. BECAUSE SOME STATES DO NOT ALLOW THE EXCLUSION OR
' LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE
' ABOVE LIMITATION MAY NOT APPLY TO YOU.






'=========================================================
'==                    From Page 5                      ==
'=========================================================

Public Sub CalcPayments()
	' place statements here
	MsgBox "I'm public!"
End Sub

Sub CalcIncome()		' Public by default
	' place statements here
	MsgBox "I'm public, too!"
End Sub

Private Sub CalcSavings()
	' place statements here
	MsgBox "I'm private!"
End Sub





'=========================================================
'==                    From Page 6                      ==
'=========================================================

totalSales = CalculateTotal(x)	'CalculateTotal is a Function procedure

'-------------------------------------------------

Function ConeSurface(radius, height)
	Const Pi = 3.14159
	coneBase = Pi * radius ^ 2
	coneCirc = 2 * Pi * radius
	coneSide = Sqr(radius ^ 2 + height ^ 2) * coneCirc / 2
	ConeSurface = coneBase + coneSide
End Function

'-------------------------------------------------

totalSurface = ConeSurface(3, 11) + 2 * ScoopSurface(3)





'=========================================================
'==                    From Page 7                      ==
'=========================================================

Function CalcPay(hours, rate) As Currency





'=========================================================
'==                    From Page 9                      ==
'=========================================================

' Both of these statements call a Sub named MyProc.
Call MyProc (firstArgument, secondArgument)
MyProc firstArgument, secondArgument

'-------------------------------------------------

Function PercentSOV(SOV, sharedMotorized, nonMotorized)
	Const nmFactor = 0.2
	totalSOV = SOV - nmFactor * nonMotorized
	totalPossibleTrips = SOV + sharedMotorized + nonMotorized
	PercentSOV = (totalSOV / totalPossibleTrips) * 100
End Function

'-------------------------------------------------

If PercentSOV(SOV, Bus, BikeOrWalk) > 72 Then Debug.Print "Too high"
Application.StatusBar = 100 - PercentSOV(SOV, Bus, BikeOrWalk)
X = PercentSOV(SOV, Bus, BikeOrWalk)





'=========================================================
'==                    From Page 10                     ==
'=========================================================

[THIRDBK.XLS].TestProc





'=========================================================
'==                    From Page 11                     ==
'=========================================================

[East Coast].TotalToDate
MidWest.TotalToDate

'-------------------------------------------------

[REGION.XLS].[Total Sales].TotalToDate
[STATE.XLS].[Total Sales].TotalToDate





'=========================================================
'==                    From Page 12                     ==
'=========================================================

Sub UpdateRecord(custId, custName)

'-------------------------------------------------

Dim newId As Integer
Dim newName As String

newId = 3452
newName = "Mary Boyd"
UpdateRecord newId, newName

'-------------------------------------------------

Sub PrintList(custArray())





'=========================================================
'==                    From Page 13                     ==
'=========================================================

Function Reverse (S As String, ByVal n As Integer)
	' Reverses the first n characters in S.
	Dim Temp As String, i As Integer
	If n > Len(S) Then n = Len(S)
	For i = n To 1 Step -1
		Temp = Temp & Mid(S, i, 1)
	Next
	Reverse = Temp & Right(S, Len(S) - n)
End Function

'-------------------------------------------------

Type custInfo
	custName As String
	custCompany As String
	custId As Integer
End Type

Sub PrintList(newCust As custInfo)
	'place statements here
End Sub

'-------------------------------------------------

Sub AddThree(ByRef passedVar As Integer)
Sub AddThree(passedVar As Integer)		'ByRef is the default





'=========================================================
'==                    From Page 14                     ==
'=========================================================

Sub TestPassingArgs()
	Dim varToPass As Integer
	varToPass = 7
	AddThree varToPass
	MsgBox varToPass		' displays 10
End Sub


Sub AddThree(ByRef passedVar As Integer)
	passedVar = passedVar + 3
End Sub

'-------------------------------------------------

Sub TestPassingArgs()
	varToPass = 7
	AddThree varToPass
	MsgBox varToPass		' displays 7
End Sub


Sub AddThree(ByVal passedVar As Integer)
	passedVar = passedVar + 3
End Sub




'=========================================================
'==                    From Page 15                     ==
'=========================================================

Sub TestPassingArgs()
	varToPass = 7
	AddThree (varToPass)
	MsgBox varToPass		' displays 7
End Sub


Sub AddThree(ByRef passedVar As Integer)
	passedVar = passedVar + 3
End Sub





'=========================================================
'==                    From Page 16                     ==
'=========================================================

Sub FormatList(startRow As Integer, startCol As Integer, _
		Optional redText, Optional sortList)
	If IsMissing(redText) Then
		redText = False
	Else
		redText = CBool(redText)
	End If
	If IsMissing(sortList) Then
		sortList = False
	Else
		sortList = CBool(sortList)
	End If
	Set myCells = Worksheets("SampleText") _
		.Cells(startRow, startCol).CurrentRegion
	If redText Then
		textColor = 3
	Else
		textColor = xlAutomatic
	End If
	myCells.Font.ColorIndex = textColor
	If sortList Then myCells.Sort key1:=myCells.Cells(1, 1)
End Sub

'-------------------------------------------------

Sub DoList()
	FormatList 2, 2, False, True
End Sub

'-------------------------------------------------

Sub DoList()
	FormatList 2, 2, , True
End Sub





'=========================================================
'==                    From Page 17                     ==
'=========================================================

Workbooks.Open "BOOK2.XLS", , , , "drowssap"

'-------------------------------------------------

Workbooks.Open fileName:="BOOK2.XLS", password:="drowssap"

'-------------------------------------------------

Workbooks.Open password:="drowssap", fileName:="BOOK2.XLS"

'-------------------------------------------------

Sub FormatList(startRow As Integer, startCol As Integer, _
		Optional redText, Optional sortList)

'-------------------------------------------------

Sub DoList()
	FormatList redText:=True, startCol:=2, startRow:=2
End Sub





'=========================================================
'==                    From Page 18                     ==
'=========================================================

Function AddInts(ParamArray intNums())
	Dim x As Integer
	Dim y As Variant

	For Each y In intNums
		x = x + y
	Next y
	AddInts = x
End Function

'-------------------------------------------------

Sub ShowSum()
	MsgBox AddInts(1, 3, 5)
	MsgBox AddInts(1, 3, 5, 7, 8, 9, 34, 98, 123)
End Sub


