+ Reply to Thread
Results 1 to 9 of 9

Thread: Range definition: counting from the 2nd row down to whenever it ends

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Range definition: counting from the 2nd row down to whenever it ends

    Hello,

    I have defined a range, which I will use later in worksheetfunction.averageifs, but I want Excel to count from the 2nd row down to whenever it ends...

    How do I put the lrow index in the range in the formula below?

    AClinicElapsedDay is the range that contains numbers to be averaged later. (I just don't want Excel to average the title heading of that column)

    code is:

    
    Public Sub CCMPeriodInternalAnalysisVCH()
    'calculating Average
    
    Dim a3 As Long
    Dim AClinicElapsedDay As Range
    
    Dim lrow As Integer
    lrow = Worksheets("A).Range("R2").End(xlUp).Row
    
    Set AClinicElapsedDay = Worksheets("A").Range("R2") ' how do I put that lrow in the range here?
    
    sub end
    Thank you
    Last edited by Lifeseeker; 01-04-2012 at 09:15 PM.

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Range definition: counting from the 2nd row down to whenever it ends

    Set AClinicElapsedDay = Worksheets("A").Range("R2:R" & lrow)

    Regards, TMS

  3. #3
    Valued Forum Contributor dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    New Delhi, India
    MS-Off Ver
    Excel 1997, 2000, XP, 2003, 2007, 2010
    Posts
    2,406

    Re: Range definition: counting from the 2nd row down to whenever it ends

    Hi Lifeseeker,

    see below to get idea to include R2 till last range.

    Sub CCMPeriodInternalAnalysisVCH()
     Dim rng As Range, i as double
    Set rng = Range(Range("r2"), Range("r2").End(xlDown))
    i = WorksheetFunction.Average(rng)
    End Sub
    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    DILIPandey
    +919810929744
    dilipandey@gmail.com

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Range definition: counting from the 2nd row down to whenever it ends

    Hey Shucks,

    Thanks it works out well.

    Just so I have you here if you don't mind, the averageifs is generating an error run-time erorr: 1004: "Unable to get the averageifs property of the worksheet function class"

    code I'm using is:

    Public Sub test()
    
    Dim a3 As Long
    Dim AClinicElapsedDay As Range
    Dim AClinicPeriod As Range
    Dim mPeriod As Range
    Dim AClinicAnalyzable As Range
    Dim lrow As Integer
    
    lrow = Worksheets("A").Range("R2").End(xlUp).Row
    Set AClinicElapsedDay = Worksheets("A").Range("R2:R" & lrow)
    
    Set AClinicPeriod = Worksheets("A").Range("N:N")
    Set mPeriod = Worksheets("TEST").Range("B2")
    Set AClinicAnalyzable = Worksheets("A").Range("W:W")
    
    a3 = Application.WorksheetFunction.AverageIfs(AClinicElapsedDay, AClinicPeriod, mPeriod, AClinicAnalyzable, "Yes")
    Worksheets("TEST).Range("A3") = a3
    
    End Sub
    Are you able to assist?

    Thank you

  5. #5
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Range definition: counting from the 2nd row down to whenever it ends

    Your ranges need to be the same size.

    Also note that it is not good practice to define variables that can be mistaken for a range, for example, a3.

    Option Explicit
    
    Public Sub test()
    
    Dim a3 As Long
    Dim AClinicElapsedDay As Range
    Dim AClinicPeriod As Range
    Dim mPeriod As Range
    Dim AClinicAnalyzable As Range
    Dim lrow As Integer
    
    lrow = Worksheets("A").Range("R2").End(xlUp).Row
    Set AClinicElapsedDay = _
        Worksheets("A").Range("R2:R" & lrow)
    
    Set AClinicPeriod = _
        Worksheets("A").Range("N2:N" & lrow)
    Set mPeriod = _
        Worksheets("TEST").Range("B2")
    Set AClinicAnalyzable = _
        Worksheets("A").Range("W2:W" & lrow)
    
    a3 = Application.WorksheetFunction.AverageIfs( _
        AClinicElapsedDay, _
        AClinicPeriod, mPeriod, _
        AClinicAnalyzable, "Yes")
    Worksheets("TEST").Range("A3") = a3
    
    End Sub


    Regards, TMS

  6. #6
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Range definition: counting from the 2nd row down to whenever it ends

    Hi Shucks,

    It is still not working....I have attached a test copy.....I think it has to do with the actual averageifs worksheetfunction itself....which may not be available from VBA??

    I have made sure that ranges are the same as well, but it's a no go...

    I'm really pulling my hair out on this one...

    In worksheet "Test", the button executes the code, but I keep getting the run-time error: 1004...
    Attached Files Attached Files

  7. #7
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Range definition: counting from the 2nd row down to whenever it ends

    It's the way you return the lrow variable.

    lrow = Worksheets("A").Range("A2").End(xlUp).Row

    gives a value of 1.

    Try:


    Option Explicit
    Private Sub CommandButton1_Click()
    'calculating Average in Days for VGH clinic, CCM by period
    Dim b5 As Double
    Dim AClinicElapsedDay As Range
    Dim AClinicPeriod As Range
    Dim mPeriod As Range
    Dim AClinicAnalyzable As Range
    Dim lrow As Integer
    
    Set mPeriod = _
        Worksheets("Test").Range("B2")
    lrow = Worksheets("A").Range("A" & Rows.Count).End(xlUp).Row
    Set AClinicElapsedDay = _
        Worksheets("A").Range("B2:B" & lrow)
    Set AClinicPeriod = _
        Worksheets("A").Range("A2:A" & lrow)
    Set AClinicAnalyzable = _
        Worksheets("A").Range("C2:C" & lrow)
    
    b5 = Application.WorksheetFunction.AverageIfs( _
        AClinicElapsedDay, _
        AClinicPeriod, mPeriod, _
        AClinicAnalyzable, "Yes")
    
    Worksheets("Test").Range("B5") = b5
    
    End Sub

    Regards, TMS

  8. #8
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Range definition: counting from the 2nd row down to whenever it ends

    ah yes, the other folk just helped me out on this one. Thank you Shucks, you are right.

  9. #9
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Range definition: counting from the 2nd row down to whenever it ends

    The other folk?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0