VBA Learning notes for cells

Source: Internet
Author: User

' Cell objects are a very basic and important part of VBA. ' It's also very diverse in its expression style. '----------------------------------------------------' Range object ' represents a cell, a row, a column, a selected range that can contain one or several contiguous cell ranges. Or a three-dimensional area. A common notation for the ' range ' (text-loaded cell address) ' range is sub Rng () range ("A1"). Select         ' cell range ("A:a"). Select        ' column range ("1:3"). Select       ' Row range ("A1:b10"). Select     ' adjacent region range ("A1:d7 c4:e8"). Select  ' does not have a neighboring region (select multiple different areas while holding down ctrl) End Sub

Range ("A1:d7 c4:e8"). Select Note Here The D7 is followed by a space to take the intersection of the two regions , if it is a comma, then take the combination of the two regions

' Notation: Range ("text-loaded cell address 1", "text-mount cell address 2") ' range ' changes to the sub rng Change () range ("A1:b10"). Select ' General notation Range ("A1", "B10"). Select ' Change notation 1Range (Range ("A1"), Range ("B10"). Select ' Change 2Range ("a1") = 123End Sub ' NOTE: ' 1. If you do not specify a worksheet before range, the default is active sheet ' 2. If the object is not an active sheet, such as an active chart, the above code will also have the error Sub Cell Object Example () Debug.Print Range ("A:a"). Count ' counts the maximum number of rows in the worksheet Debug.Print Range ("1:1"). Count ' calculates the maximum number of columns for the worksheet Debug.Print Application.counta ("a:a") ' calculates the number of rows used by the worksheet ' CountA calculates how many non-empty cells ' Debug. Print Application.counta (Range ("1:1")) ' calculates the number of columns used by the worksheet end Sub

The following is a reference to a dynamic cell (a cell that is not known beforehand) (in place of a variable)

' 1.range ("Address area"). Range ("Address area") Sub ordinal notation () range ("B2:d4"). Range ("B2"). Select ' Relative reference ' refers to the variable support () Dim a%a = 3Range ("A" & A) that supports variable sub range in the upper left cell of the previous range and the End Sub ' 2.range address area. Selectrange ("C3:e5") (2). SelectEnd SubSub Instance 1 dynamic Select cell or range () Dim i%i = Application.counta (Range ("C:c")) ' Find the last cell position used in column C ("C" & i). Select ' Selects the last range of column C ("A1", "C" & i). Select ' Selects the last A1 to column C (method one) Range ("A1:c" & i). Select ' Selects the last (method two) ' summary of the A1 to column C: the positioning of the dynamic cell range, which can be applied to the actual work of saving the document end Sub

Reference to Range:

Sub test () Range ("A1:c3") (3). Selectrange ("A1:c3") (4). SelectEnd Sub

Results The first choice is C1, the second choice is A2

Summary: Range (...) (number) The order of reference is from left to right, then from top to bottom, and number is a reference to the relative position of the front area, of course number can be (2,3) such coordinates

That is: number is a relative reference and has a certain order .

VBA Learning notes for cells

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.