Uploaded by bkbaloney

Example Code VBA

advertisement
Sub ChangePropertiesValues()
'0. Add a new worksheet before the Data worksheet
'Tip: Use the Add method of the worksheets collection
'Note: The newly added worksheet will become the active worksheet
Worksheets.Add before:=Worksheets("Data")
'1. Name the new worksheet "Example"
ActiveSheet.Name = "Example"
'2. Select cell B2 on the active worksheet
Range("B2").Activate
'3. Place the number 23 in the active cell
ActiveCell.Value = 23
'4. Place the value from cell B2 of the Data worksheet in cell B3
'of the Example worksheet. Do not use the copy method.
Worksheets("Example").Range("b3").Value = Worksheets("Data").Range("B2").Value
'or
Worksheets("Data").Range("b2").Copy Destination:=Range("b3")
'5. Place the value from cell B2 in cell B4 (both on the example worksheet)
'using the copy method
Range("b2").Copy Destination:=Range("B4")
'to not copy formatting as well
Range("B4").Value = Range("B2").Value
'6. Increase the value in cell B4 by 20
Range("B4").Value = Range("B4").Value + 20
'7. In cell B5 enter a formula for the sum of B2:B4
Range("B5").Formula = "=sum(B2:B4)"
'8. Change the font in B5 to be bold.
Range("b5").Font.Bold = True
' notice that before you run this macro again, you will need to delete the Example worksheet.
' reason: you can not have two worksheets with the same name.
'create a new sub that deletes the Example worksheet. Notice that if you run the code when
there
'is no such worksheet you get a run time error.
End Sub
Download