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
Bookmarks