+ Reply to Thread
Results 1 to 7 of 7

Help with understanding this VBA syntax: .End(3)(1)

Hybrid View

florayaoyao Help with understanding this... 07-07-2014, 08:57 PM
protonLeah Re: Help with understanding... 07-07-2014, 09:29 PM
TMS Re: Help with understanding... 07-07-2014, 11:43 PM
6StringJazzer Re: Help with understanding... 07-08-2014, 10:29 AM
florayaoyao Re: Help with understanding... 07-08-2014, 12:06 PM
madmarq69 Re: [SOLVED] Help with... 05-19-2019, 05:53 PM
6StringJazzer Re: [SOLVED] Help with... 05-19-2019, 08:20 PM
  1. #1
    Registered User
    Join Date
    07-04-2014
    Location
    denver
    MS-Off Ver
    2013
    Posts
    12

    Help with understanding this VBA syntax: .End(3)(1)

    Moderator's note: I modified your title to be more specific. Normally I would just issue a warning but in this case I sympathized. --6StringJazzer

    Hi all,

    The original questions is to create 4 quarter sheets from the annual sales sheet, and a smart guy helped me with this code. I tried this code, and it works well, but I have a hard time of understanding some portion of this code. what is the .End(3)(1) and .End(3)(2) Refer to? Any people have any idea? thank you very much !!


    Sub florayaoyao()
    Dim i As Integer
    For i = 1 To 4
    Sheets.Add.Name = "Quarter " & i
    ActiveSheet.Rows(1).Value = Sheets("Sheet1").Rows(1).Value
    Next i
    With Sheets("Sheet1")
        For i = 2 To .Range("A" & Rows.count).End(3)(1).Row
            Select Case Month(.Range("A" & i))
                Case Is = 1, 2, 3
                    .Rows(i).Copy Sheets("Quarter 1").Range("A" & Rows.count).End(3)(2)
                Case Is = 4, 5, 6
                    .Rows(i).Copy Sheets("Quarter 2").Range("A" & Rows.count).End(3)(2)
                Case Is = 7, 8, 9
                    .Rows(i).Copy Sheets("Quarter 3").Range("A" & Rows.count).End(3)(2)
                Case Is = 10, 11, 12
                    .Rows(i).Copy Sheets("Quarter 4").Range("A" & Rows.count).End(3)(2)
            End Select
        Next i
    End With
    End Sub
    Any help is appreciated!!
    Attached Files Attached Files
    Last edited by florayaoyao; 07-08-2014 at 12:15 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,971

    Re: Help with understanding this VBA syntax: .End(3)(1)

    Normally, you would see:
    Range("A1").end(xlDown or xlToLeft or xlToRight or xlUp )
    So the value in the first set of parentheses ranges from 1 to 4, corresponding to the named constants. The second set seems (?) to be an offset.
    Ben Van Johnson

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,586

    Re: Help with understanding this VBA syntax: .End(3)(1)

    I think the (1) is redundant and the (2) will give you the cell 1 row down.

    For example, Range("A1")(1,1) would be cell A1 and Range("A1")(3,2) would be cell B3

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,023

    Re: Help with understanding this VBA syntax: .End(3)(1)

    I believe protonLeah has the correct explanation. The first number in parentheses is an argument to the End attribute, and indicates which direction to go to reach the end. However, the built-in constants listed by protonLeah correspond to the following integers:

    xlDown=-4121
    xlUp=-4162
    xlToLeft=-4159
    xlToRight=-4161

    I am not sure why a 3 works here but my test shows that it does the same thing as xlUp. (In my opinion using 3 here is a poor coding practice.)

    The second number in parentheses is a Range index. Any time you have an expression that returns a Range, you can add (row[, column]) indexing after it to refer to a specific cell within that Range. The indexing is relative to the upper left corner of the range (it is an index, not an offset), and may refer to a cell outside the range. The index (1, 1) refers to the upper left corner cell. If column is omitted it defaults to 1, so (1) also refers to the upper left corner.

    Range("A" & Rows.count).End(3)(2)
    Let's work our way inside out to break this down.

    Rows.count is the number of rows in the worksheet. For 2007+ this will be 1,048,576.
    Range("A" & Rows.count) will be A1048576. Note that this is a Range.
    Range("A" & Rows.count).End(3) starts in A1048576 and moves upwards until it finds a used cell. Note that this expression returns a Range.
    Range("A" & Rows.count).End(3)(2) starts with the used cell just found, treats it as (1, 1), and then returns the cell located at (2, 1), or one row below it.

    For example, if there are 15 rows of data, the cell returned by the above will be A16.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    07-04-2014
    Location
    denver
    MS-Off Ver
    2013
    Posts
    12

    Re: Help with understanding this VBA syntax: .End(3)(1)

    Thank you all for the helps!! I got this answer! thank you 6StringJazzer, for the details!! Appreciate all!=)

  6. #6
    Registered User
    Join Date
    05-27-2013
    Location
    Corpus Christi, Texas
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: [SOLVED] Help with understanding this VBA syntax: .End(3)(1)

    Starting to work with VBA. Can you help breakdown what each section of the below command represents?
    lastRow = Workbooks(mainWB).Sheets(mainSht).Range("A" & Rows.Count).End(xlUp).Row

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,023

    Re: [SOLVED] Help with understanding this VBA syntax: .End(3)(1)

    Administrative Note:

    Welcome to the forum madmarq69.

    We are happy to help, however while you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original. Please start a new thread - See Forum rule #4

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Syntax question -- not understanding
    By scott micklo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2018, 02:29 PM
  2. Understanding the Msgbox Command and all its Syntax
    By Mark123456789 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-19-2016, 02:09 AM
  3. Understanding the syntax of certain formulas
    By jnorthway in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 08-07-2013, 10:08 PM
  4. understanding syntax
    By derwalroszsagt in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-04-2012, 01:15 PM
  5. [SOLVED] Excel 2007 : Match Syntax - Verify My Understanding
    By CatherineCarey in forum Excel General
    Replies: 2
    Last Post: 04-19-2012, 03:10 PM
  6. Understanding Step - 1 syntax in for loop. VBA
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2012, 10:34 AM
  7. Understanding syntax to average values
    By bubba57 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 02-26-2009, 11:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1