+ Reply to Thread
Results 1 to 6 of 6

find text in listbox on userform, delete row containing that text on spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2012
    Location
    florida
    MS-Off Ver
    mac 2008
    Posts
    63

    find text in listbox on userform, delete row containing that text on spreadsheet

    Hey all, had a quick question!

    I want to do something along the lines of this:
    Sub DeleteRows()
        Dim c As Range
        Dim SrchRng As Range
        Dim SrchStr As String
    
        Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
        SrchStr = InputBox("Please Enter A Search String")
        Do
            Set c = SrchRng.Find(SrchStr, LookIn:=xlValues)
            If Not c Is Nothing Then c.EntireRow.Delete
        Loop While Not c Is Nothing
    
    End Sub
    But I want it to be on a userform with a listbox. Listbox1 will contain a list from named range, and i want people to be able to click the desired deleted one, press delete, it will go to Sheet5, find that row and delete it. Is this possible?
    Thanks!
    Steve

  2. #2
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: find text in listbox on userform, delete row containing that text on spreadsheet

    Steve,

    Yes.

    Without testing the code, or seeing/understanding the contents of Listbox1, I think the code would look something like this (not much different from what you have).

    Sub DeleteRows()
        Const WSNameWithListBox As String = Empty 'Change this name to the name of your worksheet with the Listbox
        Const ListBox1Addr As String = Empty 'change this to the name of the address where Listbox1 is located in the sheet above
    '/Note: once the above constants are assigned appropriately, the If- statements that call the Message Boxes prompting you to change the above constants from Empty may be eliminated
        Dim c As Range
        Dim SrchRng As Range
        Dim SrchStr As String
        Dim WS_WithListBox as Worksheeet, WS_5 As Worksheet
        
        If WSNameWithListBox = Empty Then
            Msgbox("Error: you must enter the name of the worksheet with Listbox1 in the code (see comments in the code)")
            Exit Sub
        End If
        Set WS_WithListBox = ThisWorkbook.Worksheets(WSNameWithListBox)
        Set WS_5 = ThisWorkbook.Sheets(5)
        SrchRng = WS_5.Range("A1", WS_5.Rows.Count.End(xlUp))
    
        If ListBox1Addr = Empty Then
            Msgbox("Error: you must enter the name of the address of Listbox1 (such as "A1" if that is where your Listbox1 is located)..." & vbCr & "...in Worksheet: " & WSNameWithListBox & vbCr & " (see comments in the code)")
            Exit Sub
        End If
        SrchStr = WS_WithListBox.Range(ListBox1Address).Cells.Value
    
        Do
            Set c = SrchRng.Find(SrchStr, LookIn:=xlValues) 'note: I usually like to include the argument: LookAt:= xlPart and search by: Trim(SrchStr) (or, equivalently: What:=Trim(SrchStr) in case a user has a leading/trailing space) 
                                                                               'also note: if the xlPart argument is included, it will find: "A" just the same as "AA" or "A A" (if you're searching for: "A") so consider that whether or not this technique is appropriate for your code
            If Not c Is Nothing Then c.EntireRow.Delete
        Loop Until c Is Nothing
    End Sub
    I generally like to avoid the attribute: .ActiveSheet unless I'm referencing a newly-created worksheet or the worksheet where a macro button you're pushing because it can make the results dependent upon which sheet is active/you have open when the macro runs. But it looks like .ActiveSheet was used appropriately in your code.
    Last edited by joe31623; 02-05-2016 at 12:09 PM.
    <---If my answer helped, please click *

  3. #3
    Registered User
    Join Date
    03-16-2012
    Location
    florida
    MS-Off Ver
    mac 2008
    Posts
    63

    Re: find text in listbox on userform, delete row containing that text on spreadsheet

    I think what I was hoping to make it do was have a userform open, which would contain listbox 1. that userform would give a list of names from a table, you woudl select from teh listbox the one you wanted to delete and press the delete button. it would then search a specific worksheet for that text (would be in A column) and delete the entire row.

    does that make sense?
    Screen Shot 2016-02-05 at 8.31.36 PM.png

  4. #4
    Registered User
    Join Date
    03-16-2012
    Location
    florida
    MS-Off Ver
    mac 2008
    Posts
    63

    Re: find text in listbox on userform, delete row containing that text on spreadsheet

    hi anyone have an idea how to make this work within a userform like this?

  5. #5
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: find text in listbox on userform, delete row containing that text on spreadsheet

    I'm not too familiar with Userforms, but if you give me a cell, range of cells, or a starting cell and continuous column/row of cells, I can do pretty much anything along the lines of what you're asking about.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,543

    Re: find text in listbox on userform, delete row containing that text on spreadsheet

    Sub Show_It_To_Me()
        UserForm1.Show
    End Sub
    Private Sub UserForm_Initialize()
        ListBox1.List = Sheets("Sheet1").Cells(1, 6).CurrentRegion.Value    '<---- Change to where the data is
    End Sub

    Private Sub ListBox1_Click()
        Dim ii As Long
            With Sheets("Sheet5")
                For ii = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                    If WorksheetFunction.CountIf(.Range(.Cells(ii, 1), .Cells(ii, .Cells(ii, Columns.Count).End(xlToLeft).Column)), ListBox1) > 0 Then .Cells(ii, 1).EntireRow.Delete: Exit For
                Next ii
            End With
            Unload Me
    End Sub
    See attached
    Attached Files Attached Files

+ 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. Userform / Spreadsheet Text Wrap
    By BigPaulMc in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-22-2015, 08:07 PM
  2. [SOLVED] Userform: How do I use text/values from a spreadsheet to customize the userform?
    By bananajelly in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2014, 03:47 PM
  3. Populating userform listbox from sheet cell with multiline text
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-16-2013, 07:44 AM
  4. Read text file to ListBox and Delete rows in ListBox and re-write text file
    By Nu2Java in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-29-2012, 09:42 AM
  5. find text then delete two rows below it including row of found text
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2011, 08:05 AM
  6. Find and delete text from a cell with lots of text.
    By leonardzen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-22-2011, 10:52 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