Consulting

Results 1 to 13 of 13

Thread: Get last row in range that contains formulas

  1. #1

    Get last row in range that contains formulas

    Hello everyone
    I have range ("A2:A100") that contains formulas and the result of this formula sometimes is 0 ..
    How can I get the last row that is greater than 0 ...?
    I can do that using loops but I am searching for alternative approach using UDF for example or any other way avoiding loops

    Thanks advanced for help

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i can post a formula but here is a good tutorial:

    http://xldynamic.com/source/xld.LastValue.html


    look for formulas starting with =LOOKUP(2,1/...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thank you very much for reply ..
    I need UDF or vba code for that purpose as it is part of another code

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Option Explicit
    
    Public Function LastRowGreaterThanZero(ShtName As String, ColumnLetter As String) As Long
    Dim Cel As Range
    
    Set Cel = Sheets(ShtName).Cells(Rows.Count, ColumnLetter).End(xlUp)
    Do While Cel.Value <= 0 Or Not IsNumeric(Cel)
    Set Cel = Cel.Offset(-1)
    Loop
    
    LastRowGreaterThanZero = Cel.Row
    End Function
    Sub test_LastRowGreaterThanZero()
    Dim x
    x = LastRowGreaterThanZero("Sheet1", "A")
    x = LastRowGreaterThanZero("Sheet1", "A:A")
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    That's great Mr. Sam ..
    It seems that there is no escape from loop .. I wish I find a similar way to that code
    Sub LastNonEmptyRow()
        Dim wsData      As Worksheet
        Dim lrwsData    As Long
    
    
        Set wsData = Sheets("Data")
        lrwsData = wsData.Range("D:D").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        MsgBox lrwsData
    End Sub
    Which doesn't depend on loops ..

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    what?did you run the loop on an empty column?

    Do While Cel.Value <= 0 Or Not IsNumeric(Cel) Or Cel.Row <> 1
    Which doesn't depend on loops .
    Ain't gonna happen
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    The column has formulas till 12000 rows .. I know it is bad but I am developing it to get rid of those formulas ...

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    You marked the thread "Solved."

    Are you happy with the solution offered?

    The column has formulas till 12000 rows .. I know it is bad but I am developing it to get rid of those formulas ...
    If you want to get rid of all formulas that have results not zero. then
    For each Cel in Column
    If Cel.Value <> 0 then Cel.Value = Cel.Value

    Will replace those formulas with the result.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Thanks a lot for great help. Yes I have used the UDF that you provided and it is very good
    Thank you very much Mr. SamT
    Best Regards

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by YasserKhalil View Post
    I need UDF
    you don't need udf to return the row number of the last + non-zero cell in range A1:A100.

    PHP Code:
    =LOOKUP(2,1/(A1:A100>0),ROW(A1:A100)-ROW(A1)+1
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    Thanks a lot for sharing us the issue ..
    I already searched and found solutions by formulas but didn't find UDF for that purpose so I posted this thread. Thanks anyway for your great contribution
    Best Regards

  12. #12
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You shouldn't use a UDF but macro instead.

    Sub M_snb()
        [D1:D12500] = [D1:D12500].Value
    End Sub

  13. #13
    Thanks snb
    This code will convert the range to value .. and the UDF required to get the last row in the range (which has formulas) .. Or I missed something !!?

Posting Permissions

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