0

I wrote the below code in VBA to delete full rows if, in column U of Sheet21 (actually named "ROG Registration") if "Self Cancelled" or "Waitlisted" appear. When I'm on the ROG Registration tab with the VBA window open and run this it works perfectly.

I have a separate tab with macro buttons and I assigned this macro to the form control button, but when I click on the button nothing happens.

Any idea why? Could it be that I need to use the "ROG Registration" name within the code? ".Select" the tab first since it's on a different tab than the button?

Sub ROG_DeleteRows()

Dim r As Integer
For r = Sheet21.UsedRange.Rows.Count To 1 Step -1
    If Cells(r, "U") = "Self Cancelled" Then
         Sheet21.Rows(r).EntireRow.Delete
    ElseIf Cells(r, "U") = "Waitlisted" Then
         Sheet21.Rows(r).EntireRow.Delete
    End If
Next

End Sub
  • You need to specify the sheet every time: If Sheet21.Cells(r, "U") =... Otherwise the active sheet (i.e., the ones with the buttons) is used by default – cybernetic.nomad Nov 23 '18 at 16:43
  • @cybernetic.nomad it worked perfectly. Thank you!!! I thought that might be the issue, but wasn't certain where to edit (I'm fairly new to VBA). – Lenny Nov 23 '18 at 16:47
  • @Lenny,, you should write this If Sheet21.Cells(r, "U") = "Self Cancelled" Then instead of If Cells(r, "U") = "Self Cancelled" Then . – Rajesh S Nov 24 '18 at 9:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.