+ Reply to Thread
Results 1 to 6 of 6

Delete blank spaces from a named range

  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Delete blank spaces from a named range

    A little back-story:

    When trying to AddItem to listbox1, I got the following error:

    Run-time error '70': Permission denied
    After a bit of research, this is due to having my listbox1 RowSource bound to a named range. MSDN gave a solution here: solution, however I had a bunch of blank rows from testing. Now I'm trying to remove the blank rows from the named range with code in case users also do this. There's quite a few good examples on the net of removing blank spaces from listboxes, or from rows or columns, but I couldn't find anything specific to named ranges in Excel other than: this code, which keeps giving me errors. So, I need to know how to remove blank cells from a named range with vba. Thank you,
    Last edited by Taemex; 12-10-2014 at 03:54 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Delete blank spaces from a named range

    Sort the range, will get the blanks to the bottom.

  3. #3
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Delete blank spaces from a named range

    True, but how to remove the blank rows from the named range? When I add a new entry, it adds to the bottom of the list, past all the blank spaces. Even if I have the listbox or named range sorted, the named range will still be filled erroneously with blank spaces. Perhaps I can set the named range to exclude blanks somehow in Excel instead of using vba, such as:

    Refers To: =some code to remove blank spaces ("a1:a500")
    Or with vba. I just need some help knowing where to start. Been reading internet posts the last 2-hours without a clue.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Delete blank spaces from a named range

    Here's a couple of options,
    The first one sorts the range and renames the range.
    The second one deletes the blanks and renames the range.
    Change the column to 1 if its column A
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Delete blank spaces from a named range

    Interesting. It only seems to work on the first use. After that, it gives the following error rather the range has blank spaces or not:

    Run-time error '1004':
    No cells were found.
    I tried adding this bit to verify if there were blank cells or not before running the code and to clear the set variables, but it didn't help when blank lines were added back to the list:

    Please Login or Register  to view this content.
    I think I may have discovered part of the issue... even with "With Worksheets", this code runs off the active sheet which seems odd to me. When I went to another sheet in my workbook and inadvertently tested this code, it altered that sheet instead of the sheet it was supposed to. No matter, I Activated and Selected the proper worksheet and the code still only runs once. To get it to run again, I have to completely close the worksheet and reopen it. I've never seen this before.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Delete blank spaces from a named range

    You don't need to count the blank cells, if there are no blanks then no blanks will be deleted. I would just use the second option I gave you then,directing it to the correct worksheet.
    You can even use it in the user-form initialize code when you load the listbox, then the range will always be up to date.

+ 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: 8
    Last Post: 11-26-2015, 12:46 PM
  2. Replies: 1
    Last Post: 10-17-2014, 02:16 AM
  3. How to delete named range with #REF?
    By m3atball in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-02-2014, 02:09 AM
  4. Consolidated List from a range with blank spaces
    By dymention2 in forum Excel General
    Replies: 1
    Last Post: 12-17-2011, 04:21 PM
  5. Non blank named range from named range with blanks
    By goels in forum Excel General
    Replies: 3
    Last Post: 10-24-2011, 12:54 PM

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