Named Ranges To create a named range in VBA code you can use the Add method of the Names collection e.g., Names.Add Name:="ExamMarks", RefersTo:="=Exams!$A$2:$D$10" To select a named range that is on the active worksheet you can use Range("ExamMarks").Select To select a named range that is on another worksheet use Application.GoTo Reference:= "ExamMarks" The following doesn t work from a sheet other than the active sheet though it looks like it should Worksheets("Exams").Range("ExamMarks").Select
Named Ranges II To update the address of a named range after you have added/deleted rows just add the named range again with the updated address. If a range called ExamMarks has the address A2:D10 and you add three more rows of data then you could write Names.Add Name:="ExamMarks", RefersTo:="=Exams!$A$2:$D$13" In practice, an easier way to code this is to select the updated range and use Names.Add Name:="ExamMarks", RefersTo:= Selection or in short Names.Add "ExamMarks", Selection
Union Application.Union The Union method is used to select areas that are not adjacent, e.g., non adjacent columns Union(Columns("B"),Columns("E:H")).Select non adjacent cells Union(ActiveCell, ActiveCell.Offset(0,-3)).Select named ranges that are on the active sheet Union(Range("Food").Columns(2), Range("Drinks").Columns(2)) The Union operator The Union operator is, (comma) and can be used with the A1 referencing style as shown below Range("D1:E1, G1:J5").Select to select from the active cell to the last used cell on the sheet: Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select can be used on the rhs of an expression eg with WorksheetFunction var = WorksheetFunction.Max(Range("B2:B13"), Range("E2:E13"))
The Like operator Like is an operator that is used to match data on the worksheet which is in the form of a string e.g., If ActiveCell Like "ABC" Then Like is often used to partially match a string by using a wildcard in the expression e.g., If ActiveCell Like "A*" Then would evaluate to True if the value in the active cell begins with A The * stands for none, one or more characters. The other wildcard is? which stands for one character at the exact position e.g., Like "AA?" would match AAA AAa AAb but not AA
Range variables If you want to write the values from one range to another - the ranges must be the same size - you must use the value property e.g., Range("C22:C33").Value = Range("B2:B13").Value Typically one would use range variables: Sub test() Dim rng1 As Range, rng2 As Range Set rng1 = Range("C22:C33") Set rng2 = Range("B2:B13") rng1.value = rng2.value End Sub
Resize The Resize property of a range is used to resize its dimensions. The syntax is expression.resize(rowsize, ColumnSize) The example shows how to increase the size of a selection by one row: Selection.Resize(Selection.Rows.Count + 1).Select note that the reference to ColumnSize can be omitted if not needed
Row property For a single cell the Row property returns its Excel row number. For a selection, the Row property returns the Excel row number of the first cell in the selection, e.g., 5 in the illustration. Row is often used as part of some arithmetic. For example, if you needed to know how many blank rows are at top of the illustrated sheet ActiveSheet.UsedRange.Row - 1 would return 4
Various Calculating the number of the last row It s often necessary to calculate the last row number on a sheet, e.g., if you want to add new data to th e first empty row. Also a macro to delete rows is easier to write if you start with the last row and work upwards. An expression to determine the last Excel row number is ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count The Array function can be used to write multiple values to a range in one statement. e.g., Range("A1:D1") = Array("Name", "Phone", "Dept", "Course")
Cells property I There are advanced ways of using the Cells property. Given the selection illustrated, Selection.Cells(2,3) refers to cell C2 Because it is the second row, third column within the selection. For the same reason Range("A1:E6").Cells(2,3) refers to cell C2 this can be shortened to Range("A1").Cells(2,3) and further still to Range("A1")(2,3) This also applies to Named Ranges e.g., Range("ExamMarks")(2,3)
Cells property II It s easier to use the cells reference with a single parameter. Selection.Cells(1) refers to the first cell in a selection and this can be shortened to Selection(1) Similarly with Named Ranges Range("ExamMarks").Cells(1) or Range("ExamMarks")(1) Remember that Range("ExamMarks")(2) would go across as the range is two-dimensional. With a single cell or single column the reference is downwards Range("A20")(1) would reference A20 itself Range("A20")(2) would reference A21 You can use a variable like Range("A20")(i) with a For loop and achieve powerful and efficient code