MS. Excell VBA-#8, July 24, 2013.pdf

download MS. Excell VBA-#8, July 24, 2013.pdf

of 11

Transcript of MS. Excell VBA-#8, July 24, 2013.pdf

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 1/11

    The Rows of a Worksheet

    Rows Fundamentals

    Introduction to Rows

    We already know that a worksheet organizes its information in categoriescalled columns. To show the values in a worksheet, each column holds aparticular value that corresponds to another value in the same horizontalrange. While the values under a column should be the same type, thevalues in a horizontal range can be different. The group of values thatcorrespond to the same horizontal arrangement is called a row.

    Although each row in a list has a value for eachcolumn, it is not unusual to have empty areas undera certain column and sometimes a row would evenhave only one value even though there are manycolumns available.

    As a spreadsheet application, when Microsoft Excelstarts it creates the rows you will need. As a matterof fact, Microsoft Office Excel 2007 creates1,048,576 rows.

    Like the columns, each row is labeled. The rows arelabeled from Row 1 to Row 1,048,576. These labelsshow on small boxes on the left side of the MicrosoftExcel interface. Each box that shows the label of arow is called a row header:

    VBA Excel 2007

    MS Office 2007

    Spreadsheet Use

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 2/11

    You can use just a few of the rows for your assignment but all of them are always available.

    When using a row, you can click it or use the keyboard to get to it. You can also right-click a row.When you do this, a menu would appear:

    Identifying a Row

    To support the rows of a worksheet, the Worksheet class is equipped with a property named Rows.Therefore, to refer to a row, you can use the Worksheets collection or the worksheet object toaccess the Rows property. Another way you can refer to rows is by using the Range object.

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 3/11

    access the Rows property. Another way you can refer to rows is by using the Range object.

    To identify a row, indicate its worksheet and you can pass its number to the parentheses of theRows collection. Here is an example that refers to the 5th row of the second worksheet of thecurrent workbook:

    Sub Exercise() Workbooks.Item(1).Worksheets.Item(2).Rows(5)End Sub

    As reviewed for the columns, this code would work only if the second worksheet of the currentworkbook is displaying. If you run it while a worksheet other than the second is active, you wouldreceive an error. To access any row, omit the Workbooks and the Worksheets collections.

    As mentioned already, you can refer to a row using the Range object. To do that, pass a string tothe Range object. In the parentheses, type the number of the row, followed by a colon, followedby the number of the row. Here is an example that refers to Row 4:

    Sub Exercise() Range("4:4")End Sub

    If you want to refer to the rows more than once, you can declare a variable of type Range andinitialize it using the Set operator and assign it the range you want. Here is an example:

    Sub Exercise() Dim SeriesOfRows As Range

    Set SeriesOfRows = Workbooks.Item(1).Worksheets.Item("Sheet1").Range("4:4")

    SeriesOfRows.WhateverEnd Sub

    Identifying a Group of Rows

    A group of rows is said to be in a range if they are next to each other. To refer to rows in a range,in the parentheses of the Rows collection, pass a string that is made of the number of the row fromone end, followed by a colon, followed by the row number of the other end. Here is an example thatrefers to rows from 2 to 6:

    Sub Exercise() Rows("2:6")End Sub

    The rows of a group qualify as non-adjacent if they are or they are not positioned next to eachother. To refer to non-adjacent rows, pass a string to the Range collection. In the parentheses,type the number of each row followed by a colon, followed by the same number. Thesecombinations are separated by commas. Here is an example that refers to Rows 3, 5, and 8:

    Sub Exercise() Range("3:3, 5:5, 8:8")End Sub

    To refer to all rows of a worksheet, use the Rows name. Here is an example:

    Sub Exercise() RowsEnd Sub

    Rows Selection

    Selecting a Row

    As done with columns, you can select one row or a group of rows. You can perform selections usingthe mouse, the keyboard, or a combination of both.

    To select a row using the mouse, position the mouse on a row header. The mouse cursor wouldchange into a right-pointing arrow. Then click:

    You can also use only the keyboard. To select a row using the keyboard, make sure a box on itsright side is selected. Press and hold Shift. While Shift is still down, press the Space bar and releaseShift

    To support row selection, the Row class is equipped with a method named Select. Therefore, toprogrammatically select a row, access a row from the Rows collection using the references we sawearlier. Then call the Select method. Here is an example that selects Row 6:

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 4/11

    earlier. Then call the Select method. Here is an example that selects Row 6:

    Sub Exercise() Rows(6).SelectEnd Sub

    We also saw that you could refer to a row using the Range object. After accessing the row, call theSelect method. Here is an example that selects Row 4:

    Sub Exercise() Range("4:4").SelectEnd Sub

    When a row has been selected, it is stored in an object called Selection. You can then use thatobject to apply an action to the row.

    Selecting a Group of Rows

    You can also select more than one row at the same time. You can perform selections using themouse, the keyboard, or a combination of both.

    To select a range of rows using the mouse, click one row header and hold the mouse down. Thendrag in the direction of the range:

    To select many rows using only the keyboard, select the starting row. Press and hold Shift, thenpress either the up or the down arrow key. When the range of rows has been selected, release Shift

    You can also use a combination of the mouse and the keyboard to select one or more rows:

    To select a range of rows using a combination of the mouse and the keyboard, click one row atone end of the desired range. Press and hold Shift. Then click the row at the other end, andrelease the mouse.

    To select rows at random using a combination of the mouse and the keyboard, click one rowheader, press and hold Ctrl. Then click each desired row header. When you have selected thedesired rows, release the mouse. Each row selected would be highlighted:

    To programmatically select a range of rows, refer to the range using the techniques we saw earlier,then call the Select method. Here is an example that selects rows from 2 to 6:

    Sub Exercise() Rows("2:6").SelectEnd Sub

    To programmatically select non-adjacent rows, refer to them as we saw earlier and call the Selectmethod. Here is an example that selects Rows 3, 5, and 8:

    Sub Exercise() Range("3:3, 5:5, 8:8").SelectEnd Sub

    To programmatically select all rows of a worksheet, call the Select method on the Rows collection.Here is an example:

    Sub Exercise()

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 5/11

    Sub Exercise() Rows.SelectEnd Sub

    When many rows have been selected (whether adjacent or not), their selection is stored in anobject named Selection. You can access that object to apply a common action to all selectedrows.

    The Heights of Rows

    Introduction

    To display the contents of boxes on its right, a row uses a certain height. The height is thedistance from the top to the lower borders of the row. There are various techniques you can use tochange the height of a row, using approximations or being precise.

    Manually Heightening or Shrinking the Rows

    To manually change the height of a row, position the mouse on the lower border that separates itfrom the next row (unless it is the last row). Here is an example:

    Click, then drag up or down until you get the desired height, then release the mouse.

    You can also resize a group of rows. First, select the rows as we described above. Then positionthe mouse on the bottom border of one of the selected rows. Click and drag up or down in thedirection of your choice until you get the desired height. Then release the mouse.

    To undo this action:

    On the Quick Access toolbar, click the Undo button

    Press Ctrl + Z

    Automatically Setting the Heights of Rows

    If one of the boxes on the right side of a row header is too short or too tall, you can change theheight of the row. To do this:

    Double-click the bottom border of the row

    Click the row header or a box on that row. On the Ribbon, click Home. In the Cells section, clickFormat and click AutoFit Row Height:

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 6/11

    To undo any of these actions:

    On the Quick Access toolbar, click the Undo button

    Press Ctrl + Z

    Setting the Height Values of Rows

    You can use a dialog box to set exactly the desired height of a row or a group of rows. To specifythe height of a row:

    Right-click the row header and click Row Height...

    Click a row header or any box on its right side. Then, on the Ribbon, click Home. In the Cellssection, click Format and click Row Height...

    To specify the same height for many rows:

    Select a range of rows as we saw earlier. Right-click one of the rows (either one of the rowheaders or inside the selection) and click Row Height...

    Randomly select a group of (non-adjacent) rows. Right-click one of the row headers and clickRow Height...

    Select the rows. On the Ribbon, click Home. In the Cells section, click Format and click RowHeight...

    This would call the Row Height dialog box where you can type the desired value and click OK orpress Enter.

    To undo any of these actions:

    On the Quick Access toolbar, click the Undo button

    Press Ctrl + Z

    To support the height of a row, the Row object is equipped with a property named RowHeight.Therefore, to programmatically specify the height of a row, access the row using a reference as wesaw earlier, access its RowHeight property and assign the desired value to it. Here is an examplethat sets the height of Row 6 to 2.50

    Sub Exercise() Rows(6).RowHeight = 2.5End Sub

    Creating Rows

    Adding a New Row

    In our introduction, we saw that Microsoft Excel creates and makes available over a million rows youcan use when working on a worksheet. In the next lesson, we will see that you can use the boxeson the right sides of the row headers to create the necessary values of your worksheet. One of theresult is that, at times, you will want to create a row between two existing rows. Microsoft Excelprovides all the means you need to add one or more new rows to a list. When you add a new row,Microsoft Excel removes the last row to keep the count to 1,048,576.

    You can only insert a new row above an existing one. To insert a new row:

    Right-click the row header that will be below the new one you want to add, and click Insert

    Click the row header or any box on the right side. On the Ribbon, click Home. In the Cellssection, click the arrow under Insert and click Insert Sheet Rows

    To provide the ability to add a new row, the Row class is equipped with a method named Insert.Therefore, to programmatically add a row, refer to the row that will be positioned below the newone and call the Insert method. Here is an example:

    Sub Exercise() Rows(3).Insert

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 7/11

    Rows(3).InsertEnd Sub

    Adding New Rows

    To add more than one row, first select the rows, whether in a range or randomly. Then:

    Right-click one of the rows (whether one of the row headers or a box of one of the selectedrows) that will be below the selected rows, and click Insert

    (After selecting the rows,) On the Ribbon, click Home. In the Cells section, click the arrowbutton Insert and click Insert Sheet Rows

    If you select rows randomly (non-adjacent), a new row would be created below each of theselected rows.

    To undo any of these actions:

    On the Quick Access toolbar, click the Undo button

    Press Ctrl + Z

    To programmatically add new rows, refer to the rows that would be below the new ones, and callthe Insert method. Here is an example that will add new rows in positions 3, 6, and 10:

    Sub Exercise() Range("3:3, 6:6, 10:10").InsertEnd Sub

    Removing Rows

    Deleting a Row

    If you have a row you do not need anymore, you can delete it. To delete a row:

    Right-click the row header and click Delete

    Click the row header or any box on its right side. On the Ribbon, click Home. In the Cellssection, click Delete and click Delete Sheet Rows

    To support row removal, the Row class is equipped with a method named Delete that takes noargument. Based on this, to delete a row, access it using a reference as we saw earlier, and callthe Delete method. Here is an example:

    Sub Exercise() Rows(3).DeleteEnd Sub

    Of course, you can use either the Rows collection or the Range object to refer to the row.

    Deleting Rows

    To delete more than one row, first select the rows, whether in a range or randomly. Then:

    Right-click one of the rows (whether one of the row headers or a box on the right side of one ofthe selected rows) and click Delete

    (After selecting the rows,) On the Ribbon, click Home. In the Cells section, click Delete and clickDelete Sheet Rows

    To undo any of these actions:

    On the Quick Access toolbar, click the Undo button

    Press Ctrl + Z

    To delete a group of rows:

    Sub Exercise() Range("3:3, 6:6, 10:10").DeleteEnd Sub

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 8/11

    End Sub

    Using Rows

    Moving Rows

    As reviewed for a columns, a row can betreated as a container of values. As such, itcan be moved from its current location toanother and would carry all the values on theright side of its row header.

    To move a row, first click its row header toselect it. Position the mouse on one of thehorizontal lines of the selected row:

    Click and hold your mouse down. Drag up or down. Two horizontal lines would guide you. When youget the row to the desired location, release the mouse.

    When you move a row, its boxes move but it assumes the appropriated number based on its newlocation so the numeric sequence would be kept.

    To move a group of rows, select them. Position the mouse on one of the horizontal lines of theselection:

    Click and hold your mouse down. Drag up or down. Horizontal lines would guide you. When you getthe rows to the desired location, release the mouse. When you move the rows, their boxes move

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 9/11

    the rows to the desired location, release the mouse. When you move the rows, their boxes movebut they assume the numbers of the new location with the appropriate numeric sequence.

    Copying and Pasting Rows

    When moving one or more rows, their location changes and they keep the values on the right sidesof their row headers. Sometimes, you may not want to move the row(s) but only their values. Thismeans that you can copy the rows to the clipboard and paste them where you want.

    To copy a row to the clipboard:

    Right-click the row header and click Copy

    Click the row header. On the Ribbon, click Home. In the Clipboard section, click Copy

    After copying a row to the clipboard, all of its values are made available. To put those values onanother row:

    Right-click the target row header and click Paste

    Click the row header. On the Ribbon, click Home. In the Clipboard section, click Paste

    Cutting the Contents of Rows

    Instead of moving a row and its values, you can instead moving only its values but keep the rowwherever it is located. To support this operation, you can cut a row to the clipboard and paste itsvalues to another row.

    To temporarily move a row to the clipboard to wait to be pasted:

    Right-click the row header and click Cut

    Click the row header. On the Ribbon, click Home. In the Clipboard section, click Cut

    After cutting a row to the clipboard, if you do not want to paste it anywhere, you can press Esc. Ifyou want to paste it to another row:

    Right-click the target row header and click Paste

    Click the row header. On the Ribbon, click Home. In the Clipboard section, click Paste

    To temporarily move the contents of many rows to the clipboard to wait to be pasted, select therows as we saw earlier. Then:

    Right-click either one of the row headers or inside the selection, and click Cut

    On the Ribbon, click Home. In the Clipboard section, click Cut

    If you want to paste the values to another group of rows:

    Right-click a target row header and click Paste

    Click a row header. On the Ribbon, click Home. In the Clipboard section, click Paste

    When you paste, the values of the boxes under the original rows would be emptied.

    Hiding and Revealing Rows

    The rows of a list display their values as necessary. Sometimes, you may not need to see all therows. You can display some of the rows you need and (temporarily) hide those you do not need.You can hide one row or you can hide as many as you want.

    To hide a row:

    Right-click the row and click Hide

    Click the row header. On the Ribbon, click Home. In the Cells section, click Format, position themouse on Hide & Unhide, and click Hide Rows

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 10/11

    When a row has been hidden, its row header disappears from the numeric sequence and the linebetween the previous neighbors is thicker than the other dividing lines.

    To hide many rows, select the rows. Then:

    Right-click one of the row headers or inside the selection and click Hide

    On the Ribbon, click Home. In the Cells section, click Format, position the mouse on Hide &Unhide, and click Hide Rows

    To programmatically hide a row, first select. Then, access the Hidden property of the EntireRowobject of Selection. Here is an example:

    Private Sub Exercise() Rows("6:6").Select Selection.EntireRow.Hidden = TrueEnd Sub

    This code example will hide row 6. In the same way, to hide a group of rows, first select theirrange, then write Selection.EntireRow.Hidden = True.To reveal the hidden rows:

    Right-click any row header and click Unhide

    On the Ribbon, click Home. In the Cells section, click Format, position the mouse on Hide &Unhide, and click Unhide Rows

    Freezing One or More Rows

    When using a long list, you can scroll up and down as necessary. While scrolling down, some rowswould be disappearing from the top. When scrolling down, some rows would disappear from thebottom. If you want, you can freeze a row so that, when you scroll down, a top row the rowsabove it would not move.

    To freeze a row, click the row header of the row that will lead the moving rows. On the Ribbon,click View. In the Window section, click Freeze Panes, and click Freeze Panes.

    Splitting the Rows

    Instead of freezing the rows, you can divide the Microsoft Excel series of rows into two groups.Then you can scroll one group while the other is fixed.

    To split the rows in two groups, click a row header. On the ribbon, click View. In the Windowsection, click Split. This would display a bar.

    As mentioned for the columns, the similarities between the freezing and splitting are as follows:

    The rows are divided in two groups

    The user can scroll the rows from the bottom side of the divider

    The differences between the freezing and splitting are as follows:

    If you freeze a row, you can scroll the rows below the frozen line but you cannot scroll therows above the frozen line. If you split the rows, you can scroll the rows from either the top orthe bottom side of the splitting bar

    If you freeze a row, you cannot move the freezing line to another row (you would have tounfreeze the row, then re-freeze). If you split the rows, you can move the splitting bar up ordown

    If you freeze a row, to remove the frozen line, you use the Ribbon. If you split the rows, toremove the splitting line, you can double-click it or, in the Window section of the View tab ofthe Ribbon, you can click the Split button

    PL/SQL Excel XLSX Export

  • 24/07/13 VBA For Microsoft Excel: The Rows of a Spreadsheet

    www.functionx.com/vbaexcel/topics/rows.htm 11/11

    Home Copyright 2007-2009, FunctionX

    PL/SQL Excel XLSX Export

    www.oraexcel.com

    Create Excel xlsx files with PL/SQL from $20/yr. per DB. Download now!