Professional Documents
Culture Documents
Procedural Order..............................................................................................................................2
Open code module for a sheet..........................................................................................................2
Naming a procedure or function......................................................................................................2
Call a procedure...............................................................................................................................2
Specifying a Startup Procedure........................................................................................................3
Call a function..................................................................................................................................3
Working with Controls....................................................................................................................3
Turn off Screen Updating................................................................................................................3
Declaring Variables ........................................................................................................................4
Assigning values to variables:.........................................................................................................4
Declaring Using an Array................................................................................................................4
Select Case Statements....................................................................................................................5
Do While Loop/Do Until Loops......................................................................................................5
For Next Loops................................................................................................................................5
Referring to a combo box value.......................................................................................................6
Setting a combo box value...............................................................................................................6
Add Items to a combo box...............................................................................................................6
Clear a Como Box............................................................................................................................7
Deleting Rows..................................................................................................................................7
Opening a database connection........................................................................................................7
Closing database connection............................................................................................................8
Recordset BOF and EOF.................................................................................................................8
Move to a record in a recordset........................................................................................................8
Copying data directly from a recordset............................................................................................9
Getting values from fields in a recordset.........................................................................................9
Looping through a recordset............................................................................................................9
Message Boxes.................................................................................................................................9
Reference Overview.......................................................................................................................10
Sheet References............................................................................................................................10
Cell References..............................................................................................................................11
Row References.............................................................................................................................11
Range References...........................................................................................................................11
Setting Cell/Range Properties........................................................................................................12
With Statement...............................................................................................................................12
Formatting Lines and Borders.......................................................................................................13
Make Copy of a Sheet....................................................................................................................13
Create New Workbook..................................................................................................................13
Go to Last Cell in Data Range.......................................................................................................14
Parsing a String..............................................................................................................................14
Dynamic Array Variable:...............................................................................................................14
Find Last Non-Empty Row in Sheet..............................................................................................15
Show a User Form.........................................................................................................................15
Locking Cells.................................................................................................................................15
Procedural Order
1. Process any inputs to the subroutine
2. Declare variables
3. Do Stuff, such as:
Declare database connections and recordsets (as needed)
Open database connection and recordset
Process data using Loops, Case statements, etc. (as needed)
Assign values to variables, increment, change, etc.
Output datacopy from recordset, assign cell values, etc.
Show messages, notifications, prompts. etc.
Close connections and recordsets
4. Exit
Call a procedure
Note: procedures and functions can be called by another procedure.
Example:
Call Thingy
Call Sheets("Sheet1").Thingy if the Sub belongs to another sheet
Call a function
Example:
Call Thingy(variable)
Note: value(s) in parentheses can be used as variables in the called sub/function and dont
have to be dimmed
Declaring Variables
Note: Use Dim to declare, AS to specify data type (a good idea)
Example:
Dim comboval
Dim vSort
Dim intRecordCount As Integer
fCount = fCount + 1
ReDim Preserve FolderFiles(1 to fCount)
' declares the array variable again (size+1)
FolderFiles(fCount) = tmp
tmp = Dir
Wend
Note that the command preserve is used with ReDim to keep existing data in
the array
Note: a) Will increment X and keep going until X = N, b) the X doesnt have to be
declared.
Example:
For c = 1 To intColCount
.Cells(intRowCount, c).Value = This is Column & c
Next c
Note: another variant on this is For Each, which will loop through all values in a
series:
Example:
For Each Shp In .Shapes
do stuff
Next
ComboBox1.AddItem(Smithee)
Adding values programmatically:
Do Until objRst.EOF
comboval = objRst.Fields.Item("Org")
ComboBox1.AddItem (comboval)
objRst.MoveNext
Loop
Deleting Rows
ActiveSheet.Rows("7:65536").Delete
Syntax: [Name].Openetc.
Example:
objRst.Open "SELECT SupervisorName, Sum(ProdServ), Sum(EmpDev),
Sum(Tech), Sum(TotalCourseDays),FROM [TrnDaysSum$] WHERE Org
= '" & comboval & "' GROUP BY SupervisorName ORDER BY " &
vSort, objConn
Note: specify a) the query to be used (in quotes) and b) the connection to be used
Message Boxes
Note: Notifies user of information, or prompts user to make a choice
Syntax: MsgBox(Text)
Example (Notification):
MsgBox ("Done!")
Example (Choice):
If MsgBox("Are you sure you want to go through with this?", vbYesNo) = vbNo
Then Exit Sub
End If
Reference Overview
Notes:
Basic Syntax is [Worbook].[Sheet].[Object]
Thus.
[Sheet].Cells
[Sheet].Range
[Sheet].Rows
[Sheet].Columns
After the reference.the relevant methods or properties can be applied.
Syntax: [Sheet].[Object].[Method/Property]
Examples:
.Delete
.Value
.Format
.Formula
Etc.
Sheet References
Syntax: ActiveWorkbook.Sheets(Name or Index)
Or
Cell References
Syntax: Either numeric or literal address.
Numeric .Cells(row,column)
Literal .Range(literal address)
Examples:
Get value from a cell vMgr = ActiveCell.Value
Assign value to a cell ActiveSheet.Cells(3, 2).Value = "Total"
Or:
ActiveSheet.Range("B3").Value = "Hello World"
Row References
Syntax: ActiveSheet.Rows(reference)
Example:
ActiveSheet.Rows("7:65536").Delete
Range References
Syntax: Range([Cell1], [Cell2])
Or
Range(Cells([row],[col]),Cells([row],[col]))
Examples:
Range("E9", "E200")
Range(Cells(7, 6), Cells(intRowCount, 6))
With Statement
Note: shorthand so that the Worksheet or range part of a reference neednt be repeated for
multiple operations within it.
Syntax: With Sheets([name or number])[stuff]End With
Example:
With Sheets("ManagerDetail")
.Rows("7:65536").Delete
.Range("B3").Value = "Hello World"
End With
Next x
Parsing a String
Note: may want to use this when parsing a field that contains a delimiter--say a case
where a last name is needed from a string containing first and last name separated by a
comma.
Example:
While Cells(rowNum, colNum).Value <> ""
n = InStr(1, Cells(rowNum, colNum).Value, ",")
lastName = Left(Cells(rowNum, colNum).Value, n - 1)
Wk.Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = lastName
rowNum = rowNum + 1
Wend
ReDim MyNames(1 to Max) ' declares the array variable with the necessary
size
For iCount = 1 To Max
MyNames(iCount) = ThisWorkbook.Names(iCount)
Next iCount
Erase MyNames() ' deletes the varible contents, free some memory
End Sub
Locking Cells
Note:
first have to protect entire sheet
Example:
.Locked = True
.FormulaHidden = True
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True