Simple VBA instructions

advertisement
Introduction to VBA (move_airport)
'To activate a single cell
Range("A2").Select
'To copy the selected cell
Selection.Copy
'To move to another cell then paste
Range("B2").Select
ActiveSheet.Paste
'To turn off the dotted line
Application.CutCopyMode = False
'To select a range of cells and copy
Range("A3:A5").Select
Selection.Copy
Range("B3:B5").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'To put text into a cell
Range("a7") = "hello"
'or...
Range("a8").Select
Selection = "goodbye"
'To concatenate 2 cells
Range("a9").Select
ActiveCell.FormulaR1C1 = "=R[-1]C&R[-2]C"
'To put 2 numbers into 2 cells then add them together
Range("A11").Select
ActiveCell.FormulaR1C1 = "6"
Range("A12").Select
ActiveCell.FormulaR1C1 = "7"
Range("A13").Select
ActiveCell.FormulaR1C1 = "=R[-2]C+R[-1]C"
'To change the colour of a cell
Range("A14").Select
Selection.Interior.ColorIndex = 3
'To move to a cell relative to where you are
Selection.Offset(0, 1).Select
'To select 5th cell in the 3rd row of the active worksheet
Cells(6, 1).Select
'To put the value 100 in this cell
Selection.Formula = 100
'To retrieve a value from a cell
'use the expression
x = ActiveCell.Value
'To work with a range using a name
Dim myrange As Range
Set myrange = Range("b2:b5")
myrange.Interior.Color = vbYellow
'myrange.Sort
'To move to a cell within this range
myrange.Cells(1, 4).Interior.Color = vbBlue
Download