+ Reply to Thread
Results 1 to 11 of 11

UDF code to find last populated cell in a range of columns without using a loop technique

  1. #1
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    UDF code to find last populated cell in a range of columns without using a loop technique

    Hi. My first post, so here goes:
    Looping backwards through as many as 500 empty Columns in a Row in order to find an occupied one works fine if you've nothing else to do; but it's too slow for my liking. In a VBA Sub I can jump straight to it with an xlLeft or I can use FIND(). I have thus far failed to discover a way to do this in a UDF. Similarly, I'm having to loop to locate colour marked cells along the same Row (using .Style). Can anyone point me in the right direction or am I simply asking too much of Excel?

    Posting examples of code is a problem for me as my Mac can no longer cope with my Windows Office version after an update to Catalina. The Spreadsheet I am writing for is a laptop which I keep off the Internet completely.

    Keep your masks up!

    KGW

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    something like this, but on a Mac ???
    Please Login or Register  to view this content.
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,148

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    This will find the last entry in a row

    =INDEX(1:1,MAX(IF(A1:IF1<>0,COLUMN(A1:IF1))))

    Enter with Ctrl+Shift+Enter
    Last edited by JohnTopley; 11-25-2021 at 02:55 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  4. #4
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    Thanks for your suggestion, bsalv. Not on a Mac, though. My Excel Application is on a PC laptop. I tried the range.end(xltoleft) method in Excel 2007 a couple of years ago and found it would not work when used in a function, although it worked perfectly in a normal Sub. The same applied to the FIND() Excel function - it caused problems in a UDF.
    Have things changed since then, do you know?
    Regards
    KW

  5. #5
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    Thanks for your suggestion, John. The Index - Match - Choose complex is one I am not yet familiar with, so I found it difficult to parse your code. I take it to mean a one-dimensional array selecting the highest column number of all cells within the range A1:IF1 that are not empty. The code works when entered into a cell as an array, but how can I use it in a vba function? I also failed to find a way to copy the code down a range of 2100 cells in a column.
    Any help would be much appreciated.
    Regards
    KW

  6. #6
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    change the UDF, so that it gives the result you want
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    Thanks again, bsalv, for your quick response.
    Unfortunately, I cannot access your file as my Mac OS (on the machine that receives my email) requires Office 365, which I do not trust.
    Regards
    KW

  8. #8
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    UDF
    Please Login or Register  to view this content.
    in cell A3, you use the formula
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    Salut, bsalv.

    Tried the xlToLeft solution. So far, so good; seems to be working in the test phase at least.
    Nogmaals hartelijk dank.
    Thanks also to John Topley, whose solution I shall study in more detail.

    Regards
    KW

  10. #10
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    with the xltoleft, there is one situation, you have to take care of !!!
    That's when the last cell in that row is not empty, exceptional but it can happen.
    That's why there is that special "iif-construction"

  11. #11
    Registered User
    Join Date
    11-25-2021
    Location
    Sande, Germany
    MS-Off Ver
    2019
    Posts
    8

    Re: UDF code to find last populated cell in a range of columns without using a loop techni

    OK, bsalv; I've got that covered. Thanks.
    Regards
    KW

+ 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. VBA - For Each Loop - Cannot check all cells in range if first cell is not populated
    By RSmith93 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-01-2019, 03:00 PM
  2. Loop to last populated cell in row
    By BuZZarD73 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-03-2014, 07:21 AM
  3. [SOLVED] Macro to find empty cells in a range and repalce with the value of a populated cell
    By Markvx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2012, 06:44 AM
  4. Need to loop through all populated columns
    By Analyst_Jim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2011, 07:46 PM
  5. Do Until / Loop code to find max value in many adjacent columns?
    By vcchin in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-20-2010, 08:10 PM
  6. Find Blank Cell in Range Without Loop
    By davegugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2010, 09:57 AM
  7. Loops to find blanks then loop to find populated
    By Bevy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2006, 03:55 PM

Tags for this Thread

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