Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)

Similar presentations


Presentation on theme: "Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)"— Presentation transcript:

1 Week 4

2  Recap – Ranges  For Each Loops

3  Ranges Referencing Range Objects  Set (keyword)

4 1. Starting in the cell Current record a macro for each direction (up, down, left, right) 2. Input box – prompt the user to type l, r, u, d 3. You are going to need If..then..Else if..statements for each of the four options 4. The if statements need to take the form: 5. If (user selects direction caps OR lowercase) AND next cell in that direction is not empty THEN insert appropriate text from macro recorded earlier, ELSEIF

5  Active cell is empty ActiveCell. Value = ""  Active cell is not empty ActiveCell.Value <> “” (use ActiveCell.Offset to check surrounding cells)  If (myVar = g OR myVar = G) And another condition Then brackets are needed because the AND is dealt with before the OR

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  Rather than repeat code to perform repeat tasks, you can sometimes create a loop structure to repeat the task in hand with one line of code. The most common loop structures are:  For Each … Next  For … Next  Do … While/Do … Until

8 Sub selection_test() Selection.CurrentRegion.Select Selection.Formula = 4 Selection.Interior.Color = vbYellow End Sub Sub range_formatandfour() Dim r As Range Set r = Range("G10:h12") r.Formula = 4 r.Interior.Color = vbRed End Sub

9  Used when you want to treat each cell in the range as an individual  Particularly when the original value is required (eg New value = old value*1.2)  Or when using an If statement based on the cell value (eg For each cell in range, if the value is less than 0 change the font to red) For Each object in selection -- do some action on each object -- Next object Object is often a range variable representing a single cell

10 For each Action Next Loop When last object processed, exit loop

11  Here is an example: Sub selection_update() Dim mycell as Range ‘we assume that cells were selected before running macro ’if any cell contains non-numeric data macro will fail For Each myCell In Selection Mycell.Interior.Color = vbBlue myCell.Formula = myCell.Formula * 1.5 Next myCell End Sub

12  Another example Sub looptest() For Each myCell In Selection If myCell.Formula > 25 Then myCell.Formula = myCell.Formula * 1.5 myCell.Interior.Color = vbBlue Else myCell.Formula = myCell.Formula * 1.2 myCell.Interior.Color = vbRed End if Next myCell End Sub

13  Examples: Increment_individual_values Find_max_value  Looping worksheets/ books See code in module c

14 Sub loop_cells() Dim myRange As Range For Each myRange In Selection MsgBox myRange.Address myRange.Value = "macro passing through" Next myRange End Sub

15 Sub increment_individual_values() 'NOTE: assumes that some numeric cells are selected, will fail if a cell contains non-numeric data 'PURPOSE: multiply each individual value by 1.5 Dim acell As Range For Each acell In Selection acell.Font.Bold = True acell.Value = acell.Value * 1.5 acell.Font.Bold = False acell.Font.Italic = True Next End Sub

16 Sub find_max_value() Dim mx As Double Dim mycell As Range mx = Selection.Cells(1).Value For Each mycell In Selection If mycell.Value > mx Then mx = mycell.Value End If Next mycell MsgBox "Maximum value is " & mx End Sub

17  Text Colour  Auto Number  For Each increment

18  Using Object Variables

19 Sub MoveAbout() Dim direction As String direction = InputBox("Which way?") If (direction = "l" Or direction = "L") And ActiveCell.Offset(0, -1) <>_ "" Then Selection.End(xlToLeft).Select ' brackets are necessary because And is evaluated before Or ElseIf (direction = "r" Or direction = "R") And _ ActiveCell.Offset(0, 1) <> "" Then Selection.End(xlToRight).Select ElseIf (direction = "b" Or direction = "B") And _ ActiveCell.Offset(1, 0) <> "" Then Selection.End(xlDown).Select ElseIf (direction = "t" Or direction = "T") And _ ActiveCell.Offset(-1, 0) <> "" Then Selection.End(xlUp).Select End If End Sub


Download ppt "Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)"

Similar presentations


Ads by Google