0

I am stuck in Excel VBA code. I want my delete button to check two conditions. If all meet requirements, than delete, otherwise nothing. I have two sheets. One sheet record delete if another sheet have no any record.

Below is my current code. My second sheet name is trial balance. I want if textbox4 value check in second sheet and found and also found any value in front of 4 column, so select entry will not delete other wise delete.

I don't feel my English is very good, so I may not have described things quite the way you are expecting.

Private Sub CommandButton8_Click()
Dim I As Long
Dim W As Worksheet
Dim Z As Long
Dim cDelete As VbMsgBoxResult
Set W = Sheets("Chart of Account")
Z = W.Range("C" & Rows.Count).End(xlUp).Row
For I = 8 To Z
If W.Cells(I, 2).Value = TextBox4.Text Then
cDelete = MsgBox("ARE YOU SURE THAT YOU WANT TO DELETE THIS RECORD", vbYesNo)
If cDelete = vbYes Then
W.Rows(I).Delete Shift:=xlUp
End If
End If
Next I
metasmoke
  • 21
  • 1
  • 1
  • 3
Abid Ali
  • 1
  • 1
  • 2
    Instead of using a loop use autofilter as shown [HERE](https://stackoverflow.com/questions/11317172/delete-row-based-on-partial-text/11317372#11317372). If you still want to use a loop then do it in reverse i.e `For I = Z to 8 Step -1` – Siddharth Rout Sep 13 '20 at 00:17
  • thanks for your reply actully what i writen code is it ok but in this i want to add one more criteria for example If cDelete = vbYes Then again check one criteria in another sheet with textbox4 value if meet criteria then delete otherwise not and shown msg cant able to delete – Abid Ali Sep 24 '20 at 11:14
  • and also i want if its delete this than list automatically update in my combo box which is another form and row source it connect with this sheet – Abid Ali Sep 24 '20 at 11:18

0 Answers0