22
Excel and Visual Basic

Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Embed Size (px)

Citation preview

Page 1: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Excel and Visual Basic

Page 2: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Outline

Data exchange between Excel and Visual Basic.

Programming VB in Excel

Page 3: 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.

Page 4: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Example

Page 5: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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

Page 6: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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.

Page 7: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Security issues

VBA procedures in Excel is also called Macros.

Macros are two-edged swords. Macros viruses are malicious VBA

procedures.

Page 8: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Setting the security level

Page 9: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Security Level High (default) Medium. We

should set the security to this level to run our VBA procedure.

Low (not recommended)

Page 10: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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.

Page 11: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Example

Page 12: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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

Page 13: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Edit VBA program in Excel

Page 14: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB 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.

Page 15: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Write code in visual basic editor

Page 16: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Final result

Page 17: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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.

Page 18: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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)

Page 19: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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

Page 20: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Program task

Define a VBA function that calculate the average of a range of cells.

We define the function’s name as meanvalue.

Page 21: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

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

Page 22: Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel

Use your defined VBA function