Upload
javon-axon
View
248
Download
7
Tags:
Embed Size (px)
Citation preview
Excel and Visual Basic
Outline
Data exchange between Excel and Visual Basic.
Programming VB in Excel
Exchange data with .csv file .csv file is “comma separated value”
file. .csv file is plain text file. .csv file can only save one worksheet.
For multiple worksheets in a workbook, you need to use several .csv files.
You need do some editing to remove titles or labels before you use VB to read it.
Example
CodePrivate Sub Command1_Click()Dim payment(1 To 50) As Single, commission(1 To 50) As SingleDim index As IntegerOpen App.Path & "\commission.csv" For Input As #1
index = 1Do While Not EOF(1)Input #1, payment(index), commission(index)index = index + 1Loop
Close #1
End Sub
Using VB in Excel
Excel uses a special version of Visual Basic, which is called Visual Basic for Application (VBA)
User can use VBA to create customized Excel function (VBA procedures), which can be much more powerful than the build-in Excel functions.
Security issues
VBA procedures in Excel is also called Macros.
Macros are two-edged swords. Macros viruses are malicious VBA
procedures.
Setting the security level
Security Level High (default) Medium. We
should set the security to this level to run our VBA procedure.
Low (not recommended)
Medium level security If you set the security level to medium, every time
you open a Excel file containing macro, Excel will ask you if you want to enable the macro.
Example
VBA function code Function Comm(Sales_V As Single) as Single
If Sales_V <500 Then Comm=Sales_V*0.03
Elseif Sales_V>=500 and Sales_V<1000 Then Comm=Sales_V*0.06
Elseif Sales_V>=1000 and Sales_V<2000 Then Comm=Sales_V*0.09
Elseif Sales_V>=200 and Sales_V<5000 Then Comm=Sales_V*0.12
Elseif Sales_V>=5000 Then Comm=Sales_V*0.15
End If End Function
Edit VBA program in Excel
Insert modules The VBA procedure
should be defined in a module.
If you don‘t have an existing module, create one by using the insert->Module menu.
Write code in visual basic editor
Final result
Passing a range of cells to VBA procedure The previous example shows how to
pass one value to a VBA function. However, in Excel it is possible to
pass several cells to a build-in function. E.g. SUM(a5:a7)
We call a5:a7 is a range. User defined VBA function can also
get a range of cells as input.
Data type: range
VBA defines a specific data type: range
Range is just like a object: it has properties.
Important properties of range Count : How many cells in the range Value : Value of the cell (it works
when range refers to only one cell)
Accessing individual cells
We use a special form of For…Next loop to access each cell in a range
Function meanvalue(InputRange As Range) As Single
Dim cl As Range
For Each cl In InputRange
‘some code here to get the value
‘cl.value keep the value of the cell
Next cl
End Function
Red word are key word in Visual Basic
Program task
Define a VBA function that calculate the average of a range of cells.
We define the function’s name as meanvalue.
codeFunction meanvalue(InputRange As Range) As SingleDim cl As Range ‘cl is used to get individual cellDim index As Integer, sum As Longindex = 1ReDim inputarray(1 To InputRange.Count) As SingleFor Each cl In InputRange
inputarray(index) = cl.Value ‘save cell value into arrayindex = index + 1
Next clsum = 0For i = 1 To InputRange.Count
sum = sum + inputarray(i)Nextmeanvalue = sum / InputRange.CountEnd Function
Use your defined VBA function