Results 1 to 6 of 6

Need to delete rows that match values selected in combobox

Threaded View

  1. #1
    Registered User
    Join Date
    04-30-2015
    Location
    Newnan, GA
    MS-Off Ver
    2010
    Posts
    4

    Need to delete rows that match values selected in combobox

    This is my first post. I know next to nothing about VBA, other than what I have learned by trying and testing different bits of code. Currently I am creating a excel file that will track various tasks assigned to people in the office. I have created userforms to add and update tasks. I'm having trouble with the update form. I can pull in the information I need and update and change it, but what I want to do is to have a delete button that lets a user delete a task. Selection of the task is made by two comboboxes, the first is the client name and the second is the task. Depending on the selection, I need to then search a worksheet and find and delete the row that has both of the values the user selected. I can code it to delete based on one value (something I did in the spreadsheet that tracks the clients), but not two. I can imagine a scenario where the same task is assigned to different clients and using two values I think I can ensure that I'm deleting the right row. The code I have works sporadically, sometimes it will delete the row, but most of the time it won't. What am I doing wrong?

    This is the code I used for the two comoboboxes:

    Private Sub cmdDelete_Click()
    
    Dim colARowCount As Integer
    Dim colBRowCount As Integer
    Dim totalRows As Integer
    Dim currentRow As Integer
    Dim colA As Integer
    Dim colB As Integer
    Dim colK As Integer
    Dim colL As Integer
    Dim colACurrentValue As String
    Dim colBCurrentValue As String
    Dim colKCurrentValue As String
    Dim colLCurrentValue As String
    Dim WS As Worksheet
    
    Set WS = ThisWorkbook.Worksheets("Current")
    
        colA = 1
        colB = 2
        colK = 11
        colL = 12
        
        WS.Cells(1, colK) = Me.cmbClient.Text
        WS.Cells(1, colL) = Me.cmbTask.Text
        
        colKCurrentValue = WS.Cells(1, colK)
        colLCurrentValue = WS.Cells(1, colL)
             
        colARowCount = WS.Cells(Rows.Count, colA).End(xlUp).Row
        colBRowCount = WS.Cells(Rows.Count, colB).End(xlUp).Row
    
        totalRows = colARowCount
        If colARowCount < colBRowCount Then
            totalRows = colBRowCount
        End If
    
    ans = MsgBox("Delete this task?", vbYesNo)
            If ans = vbNo Then
                Exit Sub
            
            ElseIf ans = vbYes Then
    
        For currentRow = totalRows To 2 Step -1
            colACurrentValue = Cells(currentRow, colA).Text
            colBCurrentValue = Cells(currentRow, colB).Text
    
                   If colACurrentValue = colKCurrentValue _
                        And colBCurrentValue = colLCurrentValue Then
                            Cells(currentRow, colB).EntireRow.Delete
                    End If
                    
        Next currentRow
        
    End If
        
    MsgBox "This task has been deleted."
    Unload FormUpdateTask
    
    End Sub
    In case you're curious this is how I did it with one combobox:

    Private Sub cmdDeleteCase_Click()
    Dim ws As Worksheet
    Dim x As Long
    Dim fCell As Range
    Set ws = ThisWorkbook.Sheets("Current")
    cMatter = cbMatter.Value
            
        With ws
            Set fCell = .Range("B:B").Find(cbMatter.Value, , xlValues, xlWhole)
            
            If fCell Is Nothing Then Exit Sub
                   
                x = fCell.Row
                ans = MsgBox("Delete " & cMatter & " from list?", vbYesNo)
                If ans = vbYes Then
                    .Range("B:B").Cells(x, 1).EntireRow.Delete Shift:=xlUp
                        ElseIf ans = vbNo Then
                            Exit Sub
                                   
            End If
        
        End With
            
    MsgBox (cMatter & " has been deleted.")
    Unload FormDeleteCase
                  
    End Sub
    Last edited by patojachula; 05-06-2015 at 04:28 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hide Selected Rows that does not contain Values from combobox
    By chubbyjenz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2015, 09:10 AM
  2. Delete rows if values match in a block of cells
    By Premangshu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-21-2012, 05:47 AM
  3. Delete Selected Data on A Range Using ComboBox
    By fakehealer in forum Excel General
    Replies: 1
    Last Post: 11-10-2012, 06:24 AM
  4. Replies: 2
    Last Post: 08-28-2011, 02:39 AM
  5. Delete rows based on comboBox and offset values
    By excelpete in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2009, 03:12 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