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

' 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 22                     ==
'=========================================================

tempVal = Worksheets(1).Range("A1").Value





'=========================================================
'==                    From Page 23                     ==
'=========================================================

Sub SwapCells()
	With Worksheets(1)
		tempVal = .Range("a1").Value
		.Range("a1").Value = .Range("a2").Value
		.Range("a2").Value = temVal
	End With
End Sub





'=========================================================
'==                    From Page 24                     ==
'=========================================================

Sub CircleArea()
	Dim radius, area
	radius = InputBox("Circle Radius?")
	area = 3.14159 * radius ^ 2
	MsgBox "The area is " & area
End Sub

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

Option Explicit





'=========================================================
'==                    From Page 25                     ==
'=========================================================

Sub SwapCells()
	Dim tempVal
	With Worksheets(1)
		tempVal = .Range("a1").Value
		.Range("a1").Value = .Range("a2").Value
		.Range("a2").Value = temVal
	End With
End Sub





'=========================================================
'==                    From Page 26                     ==
'=========================================================

Sub DoTheWork
	Dim intTemp As Integer
	Static intPermanent As Integer
.
.
.
End Sub

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

Static depth





'=========================================================
'==                    From Page 27                     ==
'=========================================================

Function RunningTotal(num)
	Static accumulate
	accumulate = accumulate + num
	RunningTotal = accumulate
End Function

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

Static Function RunningTotal(num)

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

Dim intTemp As Integer
Private intTemp2 As Integer




'=========================================================
'==                    From Page 28                     ==
'=========================================================

Public intX As Integer





'=========================================================
'==                    From Page 29                     ==
'=========================================================

Dim x as Long
Private i As Integer
Private amt As Double
Static yourName As String
Public billsPaid As Currency

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

Private i As Integer, amt As Double
Private yourName As String, billsPaid As Currency
Private test, amount, j As Integer	' test and amount are Variants

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

sheetNames(1) = "sheet1"
sheetNames(2) = "module1"
sheetNames(3) = "sheet2"

MsgBox sheetNames(2)





'=========================================================
'==                    From Page 31                     ==
'=========================================================

Dim counters(14) As Integer
Dim sums(20) As Double

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

Option Base 1

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

Dim counters(1 To 15) As Integer
Dim sums(100 To 120) As String





'=========================================================
'==                    From Page 32                     ==
'=========================================================

Sub RandomArray()
	Dim i As Integer, rnums(10) As Integer

	For i = 1 To 10
		rnums(i) = Int(Rnd() * 6) + 1
	Next
	MsgBox rnums(4)
End Sub

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

Sub FillArray()
	Dim ovalLeftCoords() As Integer
	Dim i As Integer, ovalCount As Integer, ovs As Ovals
	Set ovs = Worksheets(1).Ovals
	ovalCount = ovs.Count
	ReDim ovalLeftCoords(ovalCount)
	For i = 1 To ovalCount
		ovalLeftCoords(i) = ovs(i).Left
	Next
End Sub




'=========================================================
'==                    From Page 33                     ==
'=========================================================

ReDim Preserve myArray(UBound(myArray) + 1)

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

ReDim Preserve matrix(10, UBound(matrix, 2) + 1)

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

ReDim Preserve matrix(UBound(matrix, 1) + 1, 10)

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

Static matrixA(10, 10) As Double

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

Static matrixA(1 To 10, 1 To 10) As Double

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

Dim multiD(4, 1 To 10, 1 To 15)





'=========================================================
'==                    From Page 34                     ==
'=========================================================

Dim i As Integer, j As Integer
Static matrixA(1 To 10, 1 To 10) As Double
For i = 1 To 10
	For j = 1 To 10
		matrixA(i, j) = I * 10 + J
	Next j
Next i





'=========================================================
'==                    From Page 35                     ==
'=========================================================

Const conPi = 3.14159265358979
Const conMaxPlanets = 9
Const conReleaseDate = #1/1/95#
Const conCodeName As String = "Enigma"

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

Const conPi = 3.14, conMaxPlanets = 9, _
	conWorldPop = 6000000000#

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

Const conPi2 = conPi * 2

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

Static solarSystem(1 To conMaxPlanets)
If numPeople > conWorldPop Then Exit Sub





'=========================================================
'==                    From Page 36                     ==
'=========================================================

Const conTemp As Currency = 3.45





'=========================================================
'==                    From Page 37                     ==
'=========================================================

' In Module 1:
Public Const conA = conB * 2

' In Module 2:
Public Const conB = conA / 2





'=========================================================
'==                    From Page 38                     ==
'=========================================================

Private memSize As Long
Dim loopCounter As Integer

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

Public radius As Single
Private area As Double
Dim costOfGoods As Currency





'=========================================================
'==                    From Page 39                     ==
'=========================================================

Private dataName As String

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

dataName = "Database"
dataName = Left(dataName, 4)





'=========================================================
'==                    From Page 40                     ==
'=========================================================

Dim empName As String * 50

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

Dim tempString As String * 4, dataName As String

dataName = "Database"
tempString = dataName
MsgBox tempString & " : " & dataName

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

Dim creditExceeded as Boolean

' Ask if the credit limit is exceeded.
If amt > Limit Then creditExceeded = True

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

Private today As Date





'=========================================================
'==                    From Page 41                     ==
'=========================================================

someDate = #3-6-93 13:20#
someDate = #March 27, 1993 1:20am#
someDate = #Apr-2-93#
someDate = #4 April 1993#

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

Dim wk As Object
Set wk = Worksheets(1)

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

If wk Is Nothing Then	'object variable not set





'=========================================================
'==                    From Page 42                     ==
'=========================================================

Private someValue		' Variant by default.
someValue = "17"
	' someValue contains "17" (a two-character string).
someValue = someValue - 15 
	' someValue now contains the numeric value 2.
someValue = "U" & someValue
	' someValue now contains "U2" (a two-character string).





'=========================================================
'==                    From Page 43                     ==
'=========================================================

payPerWeek = CCur(hours * hourlyPay)

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

Do
	anyNumber = InputBox("Enter a number")
Loop Until IsNumeric(anyNumber)
MsgBox "The square root is: " & Sqr(anyNumber)





'=========================================================
'==                    From Page 44                     ==
'=========================================================

Sub StringCat()
	Dim x, y

	x = "6"
	y = "7"
	With Worksheets(1)
		.Range("a1") = x + y		'concatenates values
		.Range("b1") = x & y		'concatenates values
		x = 6
		.Range("a2") = x + y		'adds values
		.Range("b2") = x & y		'concatenates values
	End With
End Sub





'=========================================================
'==                    From Page 45                     ==
'=========================================================

Dim myError
myError = CVErr(2010)

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

If IsEmpty(z) Then z = 0

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

z = Empty





'=========================================================
'==                    From Page 46                     ==
'=========================================================

z = Null

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

If IsNull(x) And IsNull(y) Then
	z = Null
Else
	z = 0
End If

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

If TypeName(x) = "Double" Then x = CSng(x) ' Convert to Single.





'=========================================================
'==                    From Page 47                     ==
'=========================================================

payPerWeek = CCur(hours * hourlyPay)





'=========================================================
'==                    From Page 48                     ==
'=========================================================

Private Type yourDataType
Public Type myDataType

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

Private Type systemInfo
	cpu As Variant
	memory As Long
	videoColors As Integer
	cost As Currency
	purchaseDate As Variant
End Type

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

Dim mySystem As systemInfo, yourSystem As systemInfo

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

mySystem.cpu = "486"
If mySystem.purchaseDate > #1/1/92# Then





'=========================================================
'==                    From Page 49                     ==
'=========================================================

yourSystem = mySystem

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

Type systemInfo
	cpu As Variant
	memory As Long
	diskDrives(25) As String		' Fixed-size array.
	videoColors As Integer
	cost As Currency
	purchaseDate As Variant
End Type

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

Type systemInfo
	cpu As Variant
	memory As Long
	diskDrives() As String		' Dynamic array.
	videoColors As Integer
	cost As Currency
	purchaseDate As Variant
End Type

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

Dim mySystem As systemInfo
mySystem.diskDrives(0) = "1.44 MB"

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

Dim allSystems(100) As systemInfo

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

allSystems(5).CPU = "386SX"
allSystems(X).DiskDrives(2) = "100M SCSI"





'=========================================================
'==                    From Page 50                     ==
'=========================================================

Private Type inputData
	wkInput as Worksheet
	rInput as Range
End Type

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

Type driveInfo
	type As String
	size As Long
End Type

Type systemInfo
	cpu As Variant
	memory As Long
	diskDrives(26) As driveInfo
	cost As Currency
	purchaseDate As Variant
End Type

Dim allSystems(100) As systemInfo
allSystems(1).diskDrives(0).type = "Floppy"

