+ Reply to Thread
Results 1 to 7 of 7

Delete range based on form search list

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Delete range based on form search list

    Greetings. Hope someone can help. My delete data form function references a list (headers) that when selected needs to delete the associated range, but keeps the list name. Code below. Without the If Not foundCell the code works fine but only deletes the first headers data. Selecting any other headers, nothing happens of course. I'm not sure how to define the foundCell as i so that it will loop through the header list if that makes sense. I'm pretty novice at VBA so please forgive my limited understanding. Oh yeah, also this is a protected sheet. I have a code that can unlock and lock it when changes need to be made to the sheet, but not sure where that would go...before the ClearContents but after the searchRange? Thank you for your time.
    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: Delete range based on form search list

    ' the lines below imply a preceeding "For i = x to y" statement which is missing
    ' it also implies that you know or calculated the number of times the combobox value is
    ' repeated in the list and that they are all in consecutive cells.

    ' since i is zero this line will cause an error when run,
    Range(Cells(i, "K"), Cells(i, "U")).ClearContents

    ' however ,since the "For i = x to y" is missing this line won't compile anyway.
    Next i
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Delete range based on form search list

    Thanks Ben. That makes sense. The range is actually discreet and the same (J1:U7) with headers in J and in consecutive order. I'm not understanding how the reference (search) from the list is not seeing the associated range that needs to be cleared. The fact that it probably has nothing to do with i probably makes it pretty elementary. I was thinking that by using i or "i + 1" that would loop through, . Please forgive me for my limited understanding with the logic.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: Delete range based on form search list

    Please Login or Register  to view this content.
    If you know that there will be more than one occurrence and that they are in consecutive rows there are several loop options to use. e.g.:
    Please Login or Register  to view this content.
    Last edited by protonLeah; 11-07-2016 at 12:50 AM.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Delete range based on form search list

    awesome! works great. So say if i wanted to transpose the range data to columns instead of rows. I altered all the code from row to columns and xlup to xltoleft, but all I'm getting is the first list item. All other items do not show up. Is there something I need to change in the foundCell line?
    Please Login or Register  to view this content.
    clear found cell command:
    Please Login or Register  to view this content.
    Last edited by terriertrip; 11-07-2016 at 03:09 AM.

  6. #6
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Delete range based on form search list

    After further investigating, it seems the problem is userform activate command:
    Please Login or Register  to view this content.
    or how do i get the userform to clear list and repopulate?:
    Please Login or Register  to view this content.
    Last edited by terriertrip; 11-07-2016 at 04:09 AM.

  7. #7
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Delete range based on form search list [SOLVED]

    I've got it to repopulate, but now I get "Run-time error '91': Object variable or With block variable not set" variable (UserForm.Activate) highlighted in the Sub cmdDeleteItem_Click(). What do I need to Dim the variable as? Please help. This is the last piece I need for my form:

    [UPDATE: There was a typo in the code "UserForm.Activate" needed to be "UserForm_Activate". Corrected code below.]

    Thank you protonLeah!

    Please Login or Register  to view this content.
    Last edited by terriertrip; 11-07-2016 at 02:10 PM. Reason: SOLVED

+ 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. Find and Delete rows based on cell value VBA, using a list as search criteria
    By ang374 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2015, 10:55 AM
  2. Replies: 17
    Last Post: 07-27-2013, 06:53 AM
  3. [SOLVED] Search for every value in a certain range and delete any row that contains any
    By TimBZKK in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-14-2013, 01:58 PM
  4. Search Range for matching value and delete.
    By morbdetro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2013, 03:02 PM
  5. Add a date range to a user form search
    By gherzberg in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2012, 11:28 AM
  6. Textbox.value from form, search on sheet2, Coloum B, and delete entire row?
    By JDExcel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-29-2012, 02:17 PM
  7. Search and delete from list
    By crowdx42 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-04-2006, 01:28 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