Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5.  Recap – For Each..Next  Personal Macro File  Immediate window  Object Variables Working with worksheets and workbooks  For Loops.

Similar presentations


Presentation on theme: "Week 5.  Recap – For Each..Next  Personal Macro File  Immediate window  Object Variables Working with worksheets and workbooks  For Loops."— Presentation transcript:

1 Week 5

2  Recap – For Each..Next  Personal Macro File  Immediate window  Object Variables Working with worksheets and workbooks  For Loops

3  For Each loops

4  Your personal macro workbook can be used to save the macros you record.  Macros in this workbook are available to you regardless of the books you currently have open.  You can access this workbook through the VBE

5  Record a new macro called myRed which formats the selected cells to red font. At the New macro window, change the Store Macro option (to Personal…)  Open the VBE and edit the macro so that the selection is made Cyan

6  You can use the Immediate window to test references and / or debug snippets of VBA code. It’s quicker than having to write a Sub just to test one or two lines.  ? in the Immediate Window ‘means print the value of’  Whereas you can execute a statement by typing it and pressing Return eg ActiveCell=99 puts 99 into the active cell on the active worksheet in the active workbook

7  Alternative For Loop syntax Sub NameRange() Dim i As Integer With Range("A1") For i = 1 To 10 Range(.Offset(i, 1),.Offset(i, 1).End(xlToRight)).Name = "Week" & i Next i End With End Sub

8  We have declared range variables  Other object variables include: Workbooks Worksheets Charts

9 Sub ObjectTest() Dim c As Range Set c = ActiveCell MsgBox "The value in the activecell is " & c.Value End Sub  You can declare variables of the type Object, where Object is a valid object reference e.g.  Dim w as Worksheet  Dim r as Range There is no Cell object  The key word Set assigns a value to an object

10  Set s = Worksheets(1)  Set s = Worksheets("exams") are valid assignments to a worksheet object  After an assignment like this you can use any method or property of the worksheet by referring to the object variable e.g. s.Activate s.Visible = Not s.Visible s.Visible = True

11  Write a macro in the VBE to declare a worksheet object, set it as Sheet 1 and rename it as Monday

12 Sub inc_rates() Dim mycell As Range For Each mycell In Selection mycell.Offset(0, 1) = mycell * 1.1 Next mycell End Sub  In a For Each loop Excel will treat a range as a single cell

13  This only applies inside the loop  In this case the mycell reference has not yet been assigned Sub inc_rates() Dim mycell As Range If IsNumeric(mycell.Value) Then For Each mycell In Selection mycell.Offset(0, 1) = mycell * 1.1 Next mycell End If End Sub

14  Once inside the loop the range takes the reference and can be referred to by any code  Corrected: Sub inc_rates() Dim mycell As Range For Each mycell In Selection If IsNumeric(mycell.Value) Then mycell.Offset(0, 1) = mycell * 1.1 End If Next mycell End Sub

15 Sub used() Dim mycell As Range For Each mycell In ActiveSheet.UsedRange mycell.Interior.Color = vbGreen Next mycell End Sub  The worksheet object has a UsedRange property, which is the range from the top left to the bottom right of cells that have been used on the worksheet.  Results example

16 Sub copytest() Range("C10").Copy Range("C1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub Sub copytest2() Range("C10").Copy Range("C1") End Sub Sub copytest2a() Range("C10").Copy destination:= Range("C1") End Sub

17  The following macro copies a cell from one workbook to another:  it can be made clearer using object variables: Sub copyrange() Workbooks("new.xls").Worksheets("sheet2").Range("A4"). _ Copy Workbooks("shapes.xls").Worksheets("Sheet1").Range("A1") End Sub

18 Sub copyrange2() Dim r1 As Range Dim r2 As Range Set r1 = Workbooks("new.xls").Worksheets("sheet2").Range("A4") Set r2 = Workbooks("shapes.xls").Worksheets("sheet1").Range("A1") r1.Copy r2 End Sub

19 Sub Newsheets() Dim n As Integer n = InputBox("How many sheets do you want?") ActiveWorkbook.Worksheets.Add Count:= n End Sub Sub AddSheetAfter() Worksheets.Add ActiveSheet.Move after:=Worksheets(Worksheets.Count) End Sub Sub AddSheetAtFront() Worksheets.Add ActiveSheet.Move before:= ' complete the argument End Sub

Similar presentations


Ads by Google