Total Pageviews

Saturday, October 27, 2018

SCOPE OF ON ERROR... STATEMENT

















The effect of ON ERROR ... ends as soon as one of the following is encountered:
  1. Another ON ERROR .... (Maybe in the form of ON ERROR RESUME x or ON ERROR GOTO x)
  2. Exit Sub / Exit Function within the same sub/function where defined.
  3. End Sub / End Function of the sub/function where defined.
IS IT BAD TO USE ON ERROR RESUME NEXT?
Yes and No.
Don't use without knowing what the effect of this statement would be. Avoid if possible. Keep the scope short wherever not possible.
To nullify the effect of an ON ERROR RESUME NEXT statement, you can call ON ERROR GOTO 0

How to Select the Intersection of Two or More Specified Ranges






















To select the intersection of the two named ranges "Address" and "Contact," you can use the following example:

Application.Intersect(Range("Address"), Range("Contact")).Select

Note that both ranges must be on the same worksheet for this example to work.

How to Select the Union of Two or More Specified Ranges





















To select the union (that is, the combined area) of the two named ranges "Address" and "Contact," you can use the following example:

Application.Union(Range("Address"), Range("Contact")).Select

Note that both ranges must be on the same worksheet for this example to work. Union method does not work across sheets. For example, this line works fine

Set y = Application.Union(Range("Sheet1!E5:F6"), _   Range("Sheet1!I9:J10"))

but this line

Set y = Application.Union(Range("Sheet1!E5:F6"), _   Range("Sheet2!I9:J10"))

returns the error message:
Union method of application class failed

How to Select a Specified Range, Offset It, and Then Resize It






















To select a range six rows below and six columns to the right of the named range "Address" and include three rows and three column more than the named range, you can use the following example:

Range("Address").Select
Selection.Offset(6, 6).Resize(Selection.Rows.Count + 3, Selection.Columns.Count + 3).Select

How to Select a Specified Range and Resize the Selection





















To select the named range "StudentDetails" and then extend the selection by six rows, you can use the following example:

Range("StudentDetails").Select
Selection.Resize(Selection.Rows.Count + 6,Selection.Columns.Count)_

    .Select

How to Select a Range of Cells Offset from a Specified Range





















To select a range of cells that is the same size as the named range "NewRecords" but that is shifted six rows down and six columns to the right, you can use the following example:

ActiveSheet.Range("NewRecords").Offset(6, 6).Select

If the named range is on another (not the active) worksheet (Sheet6), activate that worksheet first, and then select the range using the following example:

Sheets("Sheet6").Activate
ActiveSheet.Range("NewRecords").Offset(6, 6).Select

How to Select a Cell Relative to Another (Not the Active) Cell





















To select a cell that is six rows below and six columns to the right of cell F6, you can use either of the following examples:

ActiveSheet.Cells(6, 6).Offset(6, 6).Select
ActiveSheet.Range("F6").Offset(6, 6).Select

How to Select a Cell Relative to the Active Cell

















To select a cell that is three rows below and three columns to the left of the active cell, you can use the following example:

ActiveCell.Offset(3, -3).Select

To select a cell that is six rows above and six columns to the right of the active cell, you can use the following example:

ActiveCell.Offset(-6, 6).Select


Note An error will occur if you try to select a cell that is "off the worksheet." The first example shown above will return an error if the active cell is in columns A through C, since moving three columns to the left would take the active cell to an invalid cell address.

How to Select a Named Range on a Worksheet in a Different Workbook

















To select the named range "Records" on a worksheet (Sheet 5) in a different workbook (NewBook.xlsx), you can use the following example:

Application.Goto Workbooks("NewBook.xlsx").Sheets("Sheet5").Range("Records")

Or, you can activate the worksheet, and then use method 7 above to select the named range:


Workbooks("NewBook.xlsx").Sheets("Sheet5").Activate
Range("Records").Select

How to Select a Named Range on Another Worksheet in the Same Workbook
















To select the named range "Records" on another worksheet (Sheet2) in the same workbook, you can use the following example:

Application.Goto Sheets("Sheet2").Range("Records")

Or, you can activate the worksheet, and then use method 7 above to select the named range:


Sheets("Sheet2").Activate
Range("Records").Select

Thursday, October 25, 2018

How to Select a Named Range on the Active Worksheet



















To select the named range "StudentDetails" on the active worksheet, you can use either of the following examples:

Range("StudentDetails").Select

Application.Goto "StudentDetails"

How to Select a Range of Cells on a Worksheet in a Different Workbook




















To select the range E5:J10 on a worksheet (Sheet5) in a different workbook (Book5.xlsx), you can use either of the following examples:

Application.Goto Workbooks("BOOK5.xlsx").Sheets("Sheet5").Range("E5:J10")

Application.Goto Workbooks("BOOK5.xlsx").Sheets("Sheet5").Range("E5", "J10")

Or, you can activate the worksheet, and then use method 4 above to select the range:

Workbooks("BOOK5.xlsx").Sheets("Sheet5").Activate
ActiveSheet.Range(Cells(5, 5), Cells(10, 10)).Select

How to Select a Range of Cells on Another Worksheet in the Same Workbook



















To select the range E5:J10 on another worksheet (Sheet5) in the same workbook, you can use either of the following examples:

Application.Goto ActiveWorkbook.Sheets("Sheet5").Range("E5:J10")
Application.Goto ActiveWorkbook.Sheets("Sheet5").Range("E5", "J10")

Or, you can activate the worksheet, and then use method 4 above to select the range:

Sheets("Sheet5").Activate
ActiveSheet.Range(Cells(5, 5), Cells(10, 10)).Select

How to Select a Cell on Another Worksheet in the Same Workbook














To select cell F7 on another worksheet (Sheet3) in the same workbook, you can use either of the following examples:

Application.Goto ActiveWorkbook.Sheets("Sheet3").Cells(7, 6)

   -or-
 
Application.Goto (ActiveWorkbook.Sheets("Sheet3").Range("F7"))

You can also activate the worksheet, and then use method 1 above to select the cell:
Sheets("Sheet3").Activate
ActiveSheet.Cells(7, 6).Select

How to Select a Cell on a Worksheet in a Different Workbook















To select cell D4 on a worksheet (Sheet2) in a different workbook (Book2.xlsx), you can use either of the following examples:

Application.Goto Workbooks("Book2.xlsx").Sheets("Sheet2").Cells(4, 4)

    -or-

Application.Goto Workbooks("Book2.xlsx").Sheets("Sheet2").Range("D4")

You can also activate the worksheet, and then use method 1 above to select the cell:
Workbooks("Book2.xlsx").Sheets("Sheet2").Activate
ActiveSheet.Cells(4, 4).Select

How to Select a Range of Cells on the Active Worksheet
















To select the range B2:D4 on the active worksheet, you can use any of the following examples:

ActiveSheet.Range(Cells(2, 2), Cells(4, 4)).Select

ActiveSheet.Range("B2:D4").Select

ActiveSheet.Range("B2", "D4").Select

How to Select a Cell on the Active Worksheet
















To select cell C4 on the active worksheet, you can use either of the following examples:

ActiveSheet.Cells(4, 3).Select

-or-

ActiveSheet.Range("C4").Select