+ Reply to Thread
Results 1 to 4 of 4

Make UDF function flexible to any sheet

Hybrid View

YasserKhalil Make UDF function flexible to... 08-18-2015, 05:04 AM
berlan Re: Make UDF function... 08-18-2015, 05:09 AM
YasserKhalil Re: Make UDF function... 08-18-2015, 06:02 AM
berlan Re: Make UDF function... 08-18-2015, 06:14 AM
  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Make UDF function flexible to any sheet

    Hello everybody
    I have devised this udf function that brings the address of the last non zero of specific range
    Function LastNonZero(Rng As Range) 
        Dim LastRow As Long, lCol As Long, X As Long, I As Long, Cell As Range 
         
        For Each Cell In Rng.Columns 
            If Cells(Rows.Count, Cell.Column).End(xlUp).Row > X Then X = Cells(Rows.Count, Cell.Column).End(xlUp).Row: lCol = Cell.Column 
        Next Cell 
         
        LastRow = Rng.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
        For I = LastRow To 1 Step -1 
            If Cells(I, lCol) > 0 Then Exit For 
        Next I 
         
        LastNonZero = Cells(I, lCol).Address 
    End Function
    =LastNonZero(A1:B100)
    It is working well but for the activesheet only
    How it can be fixed to deal with any sheet specified in the parameters ..or I imagine
    =LastNonZero(Sheet1!A1:B100)
    If Sheet1! existed so the function would deal with Sheet1 and if missing to deal with the active sheet
    I tried to fix it but I failed

    Thanks advanced for any help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Make UDF function flexible to any sheet

    try (untested)
    Function LastNonZero(Rng As Range)
    
        Dim LastRow As Long, lCol As Long, X As Long, I As Long, Cell As Range
    
        For Each Cell In Rng.Columns
            If Rng.Parent.Cells(Rows.Count, Cell.Column).End(xlUp).Row > X Then X = Rng.parent.Cells(Rows.Count, Cell.Column).End(xlUp).Row: lCol = Cell.Column
        Next Cell
    
        LastRow = Rng.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For I = LastRow To 1 Step -1
            If Rng.Parent.Cells(I, lCol) > 0 Then Exit For
        Next I
    
        LastNonZero = Rng.Parent.Cells(I, lCol).Address
        
    End Function
    If you only write Cells( ...) it will give you the references on the activesheet. rng.parent = the sheet from the range, so I added that. See how it works, no other changes..

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Make UDF function flexible to any sheet

    Mr. Berlan
    I struggled for two hours trying to fix it and you fixed it in a minute with just one magic word "parent"
    Thank you very much for this great really great help
    Regards

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Make UDF function flexible to any sheet

    You're welcome buddy, and thanks for the kind feedback and rep

+ 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. Replies: 3
    Last Post: 04-09-2015, 08:00 PM
  2. [SOLVED] InputBox Function not as flexible as needed
    By dystopianprotagonist in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-04-2013, 10:59 PM
  3. [SOLVED] How to make a flexible line chart in Excel
    By BNCOXUK in forum Excel General
    Replies: 3
    Last Post: 10-30-2012, 12:26 PM
  4. conditional sum: can I make the array flexible
    By jash147 in forum Excel General
    Replies: 4
    Last Post: 11-02-2007, 02:25 AM
  5. [SOLVED] Make the row limit of excel flexible -not fixed at 65536.
    By quadfan in forum Excel General
    Replies: 1
    Last Post: 11-30-2005, 10:15 PM
  6. [SOLVED] Can I make formulas more flexible?
    By George in forum Excel General
    Replies: 3
    Last Post: 11-08-2005, 01:40 PM
  7. [SOLVED] How to make a flexible cell reference?
    By Mar Vernooy in forum Excel General
    Replies: 2
    Last Post: 07-01-2005, 09:05 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