You are on page 1of 10

Microsoft Excel with Macros (L2)

1
Working with VBA editor

2
Swap Values (Working with Variables)
Sub SwapNos()
Dim temp As Double
temp = Range("A4").Value
Range("A4").Value = Range("B4").Value
Range("B4").Value = temp
End Sub

3
4
Exercise
• Write your First Name in cell A4
• Write your Surname in cell B4
• Modify the code to support text swaping
• Swap the values of A4 and B4

5
Variable, Functions, Comment….
Define Variable
Dim i As Integer, Dim J as String

Mark as comment using - '

Function
Range("A1").Value – Allow to access, change value
of cell A1
Cells(i, 3).Value – Allow to access, change value of
cell 1,2,3,4… in column 3

6
Working with Loops
Sub ForLoop()
Dim i As Integer
' Loop runs
For i = 1 To Range("A1").Value
Cells(i, 3).Value = i
Next i
End Sub

7
8
Exercise
• Print ALL numbers between 1 to 100 using
FOR loop
• Print EVEN numbers between 1 to 100 using
FOR loop
• Print ODD numbers between 1 to 100 using
FOR loop
• Print Fibonacci series between 1 to 100 using
FOR loop

9
Summary….
• VBA is Excel’s macro language
• Functions return values
• Subroutines carry out procedures

10

You might also like