0% found this document useful (0 votes)
19 views10 pages

What Is VBA

Here are the steps to create a staff list with personal worksheets: 1. Create a worksheet called "Staff List" to store staff names 2. Add a loop to iterate through each name on the "Staff List" sheet 3. Inside the loop, copy the "Staff Template" sheet and rename it to the current staff name 4. Update the copied sheet with the current staff's personal details (retrieved from another sheet or variables) 5. Loop to the next staff until all names are processed 6. Optionally, hide the "Staff Template" sheet to keep the workbook clean This allows easy management of staff data by having each person's information isolated on their own worksheet, while

Uploaded by

Binh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
19 views10 pages

What Is VBA

Here are the steps to create a staff list with personal worksheets: 1. Create a worksheet called "Staff List" to store staff names 2. Add a loop to iterate through each name on the "Staff List" sheet 3. Inside the loop, copy the "Staff Template" sheet and rename it to the current staff name 4. Update the copied sheet with the current staff's personal details (retrieved from another sheet or variables) 5. Loop to the next staff until all names are processed 6. Optionally, hide the "Staff Template" sheet to keep the workbook clean This allows easy management of staff data by having each person's information isolated on their own worksheet, while

Uploaded by

Binh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 10

What is VBA?

• VBA stands for Visual Basic For Applications also known as a


programming language and is often programmed on office applications
such as Word, Excel, PowerPoint,... It can be understood that all the
languages used in Excel all uses VBA language.
For example, below is a complete line of code to assign the value 100
to file "Book1.xlsx", sheet "Sheet1", range "A1"
Specify the operation Workbook name : "Book1.xlsx"
Application.Workbooks("Book1.xlsx")

Specify the operation Worksheet name: "Sheet1“


Application.Workbooks("Book1.xlsx").Worksheets("Sheet1")

Finally, point to Range "A1" of sheet "Sheet1" and assign it to 100 as


follows:
Application.Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1
").Value = 100
Task 1: Hello World
Sub SayHello()
MsgBox (“Hello world”)
‘MsgBox(“Hello”) this is a comment
End Sub
Note: Hot key: Ctrl + Space for suggesting
Task 2: Increasing & Decreasing button

Create 2 buttons (Higher and Lower)


Define a new function

Sub higher_value()

Selection.Value = Selection.Value +1

End Sub

Sub lower_value()

Selection.Value = Selection.Value -1

End Sub

Assign 2 functions to 2 buttons that were created in last step

Test your results


Task 3: Change active cell
Create 5 buttons (Up, Down, Left, Right, Home), click there to move the active cell.

Sub Home()

Worksheets (“Sheet2”).Range(“H8”).Select

End Sub

Sub Up()

Selection.offset(-1,0).Select

End Sub

Selection.offset(-1,0).Select = UP

Selection.offset(1,0).Select = DOWN

Selection.offset(0,1).Select = RIGHT

Selection.offset(0,-1).Select = LEFT
Task 4: Simple Loop and variables

Sub SimpleLoop()

Dim i As Interger

For i=1 To 10

Selection.offset(i-1,0)=i

Next

End Sub
Task 5: Create Sheet/Copy Sheet

Sheets(“Du Lieu”).Copy Before:=Sheet(2)

Sheets(“Du Lieu”).Copy After:=Sheet(1)

MsgBox ActiveWorkbook.Sheets.Count

Sheet(3).delete

Application.DisplayAlerts = False //ignore delete warning

Sheet(“Du lieu”).delete
Task 6: Delete
Test these statements

Get specific value and rename worksheet

ActiveSheet.Name = Sheet(1). Range(“B8”)

Delete many worksheets using loop

Sub DeleteManyWorksheets()

Dim ws as Worksheet

Application.DisplayAlerts = False

For Each ws In ActiveWorkbook.Sheets

If ws.Index >2 Then

ws.delete

End if

Next ws

End Sub
• Case study: Provide a list of staffs; for each staff,
create a worksheet that contains his/her personal
information.

You might also like