Hi everyone,
I need to create a VBA form that deletes defined cells in Excel with a code like this one : Range("A1, B1, C1, D1, E1").ClearContents. I want to add checkboxes to the form, each permitting to delete a different row (1st checkbock would delete the first row, 2nd checkbox would delete the second row and so on). The change would only occur when the OK button is pressed (several rows could then be deleted at the same time if their checkboxes are checked). Since I have very limited backgroud in VBA, I don't know how to code this so the change can only happen once the OK button is clicked. Do you have suggestions? Any help is much appreciated.
Narfcois
Hi,
Not quite sure why you mention cells on row 1 and then talk about deleting rows 1,2, etc. but assuming I've interpreted correctly, put your code in the Click event of the OK Command Button. Untested but something like
etc. If there are many checkboxes you might be better to write a loop to process them rather than repeat many lines of similar syntax to the above.IF Userform1.Checkbox5 Then Sheet1.Range("E1")..EntireRow.ClearContents IF Userform1.Checkbox4 Then Sheet1.Range("D1")..EntireRow.ClearContents IF Userform1.Checkbox3 Then Sheet1.Range("C1")..EntireRow.ClearContents IF Userform1.Checkbox5 Then Sheet1.Range("B1")..EntireRow.ClearContents IF Userform1.Checkbox5 Then Sheet1.Range("A1")..EntireRow.ClearContents
I generally process these things in descending order. It doesn't matter in this example since you are only clearing contents, but on other occasions you may be deleting rows and then the order is important and it's a good habit to get into. If you want to delete say rows 2 & 4 and the macro processed row 2 first then what was row 4 becomes row 3 before the next instruction to delete a row is activated, and the second instruction will then still delete row 4 which contains what were row 5 values.
HTH
Richard Buttrey
If this was useful then please rate it appropriately.
Click the small star iconat the bottom left of my post.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks