+ Reply to Thread
Results 1 to 30 of 30

Find LastRow (based off a specific column) in a Named range and select those cells

  1. #1
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Find LastRow (based off a specific column) in a Named range and select those cells

    I have a meal planner book I'm working where I have 4 separate named ranges each with 9 rows. Each of these (breakfast, lunch, dinner, snacks) are defined in name manager. Not all 9 rows will be used for each day/meal. What I want to be able to do is to select only the rows in each of these named ranges where column C contains data. I have used LastRow in several other workbooks, however all columns in those other ranges all contained data, this is not the case for this sheet. See below for sample breakfast range and code I am trying.

    BK.PNG


    Please Login or Register  to view this content.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    It's Column C ytou want

    Please Login or Register  to view this content.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Thank you Keebellah, I should have pasted in the whole image of the sheet. yes, that would work if the breakfast was the only named range on the sheet. My issue is that I have 4 named ranges on this sheet. I have to be able to select only those rows in each of the named ranges that contain data in column C and copy them over to another sheet I have for a historical log.

    See image pasted here for a better idea of what I have.

    Attachment 682744
    Last edited by lday75; 06-16-2020 at 09:12 AM.

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Attachment not available in this manner, please attach file the 'normal' way as indicated in the forum rules please

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Find LastRow (based off a specific column) in a Named range and select those cells


    Hi,

    it could be so easy if you use Excel tables or named ranges but it seems you missed the top page yellow banner …

  6. #6
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Sorry folks, and yes I did miss that banner! I have attached the workbook now. Thanks
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    I have a sub above the problem sub which clears some other named ranges without issue. The BOLD line below is where I am getting the error "Method 'Range' of object_Global" failed. Therein lies my headache.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Why isn't the macro you wrote in your file? And the macro is incomplete missing whatever happens after Selection.Copy

  9. #9
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Your file is not representative since you do NOT have named ranges in that file.
    Please make sure you've got the whole thing as YOU have it, don't make us make assumptions or guessing work to solve your incomplete data

  10. #10
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Not sure why the module did not come over, I uploaded the .xlsm file. i will try to attach just the vb module this time. As for the rest of the code in the sub, it is all commented out, the only thing really missing is the End Sub. I copied this sub over from another workbook where I have similar code running, so the commented code does not pertain to the issue I am having, the entire sub block is:

    Please Login or Register  to view this content.

  11. #11
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    I'm return your own file with some format amendments (hiding 0 values) and a screenshot of the named ranges you say you have defined, well, they're not there.
    I do understand what you're trying to do.
    The code could use som pimping but it does what it should do, only selecting the last row in the Food Db is not necessary to do that with a loop.

    I didn't do anything for the erro yet but this is just to point out that when you post something test it so see if it's complete, as I mentioned guessing and assumptions are not an option when working with for us unknown data
    Attached Images Attached Images
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    That's very odd, because in my book those named ranges are there, image attached, and I will attach the workbook again, no clue why it did not upload a complete file!

    Attachment 682750
    Attached Files Attached Files

  13. #13
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    NOW they're present,

    I'll see what I can do or if I have a better way

    BTW, I don't know how you're attaching the image file but the way you did it is invalid
    Attached Images Attached Images

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    How about somehting like


    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  15. #15
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Thank you. I used the Insert Image button on the toolbar for the post, didn't know that was invalid, it's been a while since I posted out here.

  16. #16
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    @mikerickson stated the correct thing use the SpecialCells(xlCellTypeConstants) that way you don't need two named renage like you have now, ClearLunch and Lunch
    if so select the rage using the special cells type constant and do clearcontents it only removes data NOT formulas

  17. #17
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Thank you Mike, when I used the code below, I got the error "Unable to get the SpecialCells property of the Range class

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    I gave that a go and was presented another error, I posted to @mikerickson post with the error. I am not worried about the clear contents procedure, that works fine. My problem is that I want to copy only the rows in each named range that has data in C over to the Historical Log sheet. If each meal was on it's own worksheet this would not be an issue, however I did not want 4 different sheets for each meal.

  19. #19
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    I see Mike posted something similar to what I have been working on. The problem with his code may be sheet references and what sheet is active when you run the code. Anyway, here is what I came up with that works on your sample... it copies all the named ranges (Breakfast, Lunch, Dinner and Snacks) all at once, one under the other. Note, though, while I am only copying Columns A:O from your "Daily Log" sheet, Column P on the "Historical Log" sheet is inheriting the "Week Ending" date from the rows above because of the table structure, so you will have to add a line of code to correct that date after you run the one-liner in my macro.
    Please Login or Register  to view this content.

  20. #20
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Same theme try the code

    Only for the Breakfast, so enter something and run your macro, the same can then be used for the other ones
    Attached Files Attached Files

  21. #21
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Rick, that works great, thank you. One question, is there a way to paste values only using this method? This is bringing over the grid borders with it. If not, then I can adjust the History table to include grid lines as well.

    Thanks so much.

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Check my code, I copied the values and the formtaiing

  23. #23
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Keebellah, thank you, and yes, this is working as well, with only 1 caveat, it is not grabbing the first row in the range when copying.

  24. #24
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    After the loop add -1 at the frstrow

    Please Login or Register  to view this content.

  25. #25
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Quote Originally Posted by lday75 View Post
    One question, is there a way to paste values only using this method? This is bringing over the grid borders with it.
    The easiest way to handle this is simply remove all borders in the UsedRange...
    Please Login or Register  to view this content.

  26. #26
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Received "wrong number of arguments" error when adding the -1 to

    Please Login or Register  to view this content.

  27. #27
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Did you get a chance to try the new code I posted in Message #25 yet?

  28. #28
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Hi Rick,

    This is PERFECT, thank you. I have decided to use your solution as the code is much cleaner and works exactly how I was expecting, and you turned me onto the Intersect function, which I have not used before. GREAT. My new code block is now:

    Please Login or Register  to view this content.

  29. #29
    Forum Contributor
    Join Date
    07-03-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    Keebellah,

    Thank you for all your assistance today, I do appreciate it; however I have decided to use Rick's suggestion as it works just as I was expecting. Again, thank you.

  30. #30
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Find LastRow (based off a specific column) in a Named range and select those cells

    No problem, it's the result that counts

+ 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. [SOLVED] How to find text then select RANGE form the text found to lastrow of Column J
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2014, 02:45 AM
  2. Select Range In Column Based On Two Different Cells
    By Regular_Joe in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 05-30-2014, 11:47 AM
  3. Find & select 1st blank cell in a range of formulated column cells
    By jnbroek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 09:26 AM
  4. Fill down cells in column based on named range
    By Pademba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2013, 02:34 PM
  5. Copy from named range down until lastrow?
    By jazbath in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-19-2007, 05:56 PM
  6. Find and select LastRow Minus 1
    By Carlie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2006, 05:35 PM
  7. Select cells based on named Range and a column heading
    By achidsey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2005, 11:05 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