week 5. recap – for each..next personal macro file immediate window object variables working...

27
Week 5

Upload: reanna-ricks

Post on 15-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Week 5

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

Recap – For Each..Next Personal Macro File Immediate window Object Variables

• Working with worksheets and workbooks For Loops

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

For Each loops

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

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

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

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

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

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 egActiveCell=99puts 99 into the active cell on the active worksheet in the active workbook

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

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" & iNext i

End With

End Sub

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

We have declared range variables Other object variables include:

• Workbooks• Worksheets• Charts

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

Sub ObjectTest()Dim c As RangeSet c = ActiveCellMsgBox "The value in the activecell is " & c.ValueEnd 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

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

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

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

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

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

Sub inc_rates()Dim mycell As RangeFor Each mycell In Selection mycell.Offset(0, 1) = mycell * 1.1Next mycellEnd Sub

In a For Each loop Excel will treat a range as a single cell

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

This only applies inside the loop In this case the mycell reference has

not yet been assigned

Sub inc_rates()Dim mycell As RangeIf IsNumeric(mycell.Value) Then For Each mycell In Selection mycell.Offset(0, 1) = mycell * 1.1 Next mycellEnd IfEnd Sub

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

Once inside the loop the range takes the reference and can be referred to by any code

Corrected:

Sub inc_rates()Dim mycell As RangeFor Each mycell In Selection If IsNumeric(mycell.Value) Then mycell.Offset(0, 1) = mycell * 1.1 End IfNext mycellEnd Sub

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

Sub used()Dim mycell As RangeFor Each mycell In ActiveSheet.UsedRange mycell.Interior.Color = vbGreenNext mycellEnd 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

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

Sub copytest()Range("C10").CopyRange("C1").SelectActiveSheet.PasteApplication.CutCopyMode = False

End Sub

Sub copytest2()Range("C10").Copy Range("C1")

End Sub

Sub copytest2a()Range("C10").Copy destination:= Range("C1")

End Sub

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

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

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

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

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

Sub Newsheets()Dim n As Integern = InputBox("How many sheets do you want?")ActiveWorkbook.Worksheets.Add Count:= nEnd Sub

Sub AddSheetAfter()Worksheets.AddActiveSheet.Move after:=Worksheets(Worksheets.Count)End Sub

Sub AddSheetAtFront()Worksheets.AddActiveSheet.Move before:= 'complete the argumentEnd Sub

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

Sub AddSheetAnyMinute()Dim newSheetName As StringnewSheetName = Format(Now, "dd_mm hh_mm")Worksheets.Add.Name = newSheetNameEnd Sub

Sub AddSheetDaily()Dim newSheetName As StringnewSheetName = Format(Now, "dd_mm")Worksheets.Add.Name = newSheetNameEnd Sub

Sub AddSheetWithName()Dim newSheetName As StringnewSheetName = ActiveCell.ValueWorksheets.Add.Name = newSheetNameEnd Sub

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

Add some blank sheets and some chart sheets to your week5 workbook

Write a sub that returns a message box showing the number of worksheets and the number of sheets in the active workbook.

  Write a macro to copy the team

names from the results sheet to a new sheet , "AutumnResults"

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

You have 3 weeks to complete this task The aim is to provide you with a useful

basis for future tasks, so you may adapt it to suit your own circumstances (as long as you explain changes in the comments). When complete, e-mail it to me. Name the file “Ass2 your name”

Create a macro that, if saved to your personal macro workbook would be able to quickly format a series of worksheets and insert formulas etc.

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

Look at the workbook Jan 05• It has a series of weekly sheets that will

hold sales data for a number of products and stores

• The headings are formatted and there are formulae in place to calculate totals

• (There is a final sheet with formulas for displaying the weekly totals) - extension

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

The user should be able to open a new workbook, run your macro input the number of required products and stores and the month/ year.

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

You can extend this idea to create worksheets that would be more useful to you

You might want to extend the idea of the summary sheet – for example can you use a formula to show the product which sold the most? Can you view each week’s totals side by side

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

Remember that you can use the help files, textbooks, recorded macros and the internet for tracking down elusive code

Formulas – in this case, as you want to run a macro once and then use the workbook, use Formulas placed in cells rather than values calculated in the code

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

Do Loops