Deleting cells based on cell value of samerow
Solved/Closed
Related:
- Deleting cells based on cell value of samerow
- How to insert rows in excel automatically based on cell value without vba ✓ - Excel Forum
- Based on the cell values in cells b77 - Excel Forum
- Based on the values in cells b77 b81 what function can automatically return the value in cell c77 ✓ - Excel Forum
- If a cell has text then return value ✓ - Excel Forum
- Looking For a Value in a Cell - Excel Forum
1 response
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
May 17, 2010 at 09:56 AM
May 17, 2010 at 09:56 AM
Use the sheet event
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
May 20, 2010 at 09:12 AM
By your answer I figured that you thought I should be able to come with the answer myself. So I looked throught the codes you have given me and found the loop code I discarded before. I have adjusted it and it works fine. For those reading this post and like to see the code here it is:
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False 'if you want to find out the last used cell in column M llastrow = Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row 'in case you want to hard code 'llastrow = 1040 processrow = 4 Do While (processrow <= llastrow) If (Cells(processrow, "D") = "") Then Cells(processrow, "C").ClearContents Cells(processrow, "E").ClearContents Cells(processrow, "F").ClearContents Cells(processrow, "G").ClearContents End If processrow = processrow + 1 Loop Application.EnableEvents = True End SubThanks and best regards,
Trowa
May 20, 2010 at 09:34 AM
After saving my workbook it's not working anymore!
I only added column A to be cleared as well, can you have a look at my workbook?
https://authentification.site/files/22535057/Copy_of_PostPlanning.xls
The code is for sheet "Alle opdrachten"
Thanks in advance,
Trowa
May 20, 2010 at 09:39 AM
Looking at your solution, just a suggestion
Cells(processrow, "C").ClearContents
Cells(processrow, "E").ClearContents
Cells(processrow, "F").ClearContents
Cells(processrow, "G").ClearContents
can be consolidated into
Range(Cells(processrow, "C"), Cells(processrow, "G")).ClearContents
D is included in range but it will not matter as you have already disabled the events at the top.
I am also a bit confused over need for a do loop
Would not this be a shade better ?
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False 'if you want to find out the last used cell in column M llastrow = Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row 'in case you want to hard code 'llastrow = 1040 processrow = 4 For Each Cell In Target If Cell.Row < processrow Then GoTo Next_Cell ' only we need to work if row is row 4, 5, and up If Cell.Column <> 4 Then GoTo Next_Cell 'only we need to work if column is D If Cell <> "" Then GoTo Next_Cell ' only we need to work if cell value in D is "" 'define the range from one column to left till 3 columns to right of current cell Range(Cell.Offset(0, -1), Cell.Offset(0, 3)).ClearContents Next_Cell: Next Cell Application.EnableEvents = True End SubMay 20, 2010 at 09:49 AM
It works for me. though it is slow coz of do loop. It is processing rows that are not impacted by change. I think my solution would be a better fit here. You would need add A to my code as a separate line, as you dont want to clear B
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False 'if you want to find out the last used cell in column M llastrow = Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row 'in case you want to hard code 'llastrow = 1040 processrow = 4 For Each CELL In Target If CELL.Row < processrow Then GoTo Next_Cell ' only we need to work if row is row 4, 5, and up If CELL.Column <> 4 Then GoTo Next_Cell 'only we need to work if column is D If CELL <> "" Then GoTo Next_Cell ' only we need to work if cell value in D is "" 'define the range from one column to left till 3 columns to right of current cell Range(CELL.Offset(0, -1), CELL.Offset(0, 3)).ClearContents CELL.Offset(0, -3).ClearContents Next_Cell: Next CELL Application.EnableEvents = True End SubMay 21, 2010 at 08:58 AM
Don't you have to define the words "CELL" and "Target"? How does Excel know what to do with the lines "For Each CELL In Target" and "Next CELL"?
"Target" is a range according to the first codeline, but we didn't tell Excel what range, right?
Anyway, thanks for another great code.
Best regards,
Trowa