can i delete a row in excel based on cell value python code example

Example 1: excel vba how to get the old value of a changed cell

'Place the following in the CODE MODULE of a WORKSHEET 
'to track the last value for every cell in the used range:

Option Explicit

Private r As Range
Private Const d = "||"

Public Function ValueLast(r As Range)
    On Error Resume Next
    ValueLast = Split(r.ID, d)(1)
End Function

Private Sub Worksheet_Activate()
    For Each r In Me.UsedRange: Record r: Next
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    For Each r In Target: Record r: Next
End Sub

Private Sub Record(r)
    r.ID = r.Value & d & Split(r.ID, d)(0)
End Sub

'----------------------------------------------------------------------
'And that's it.

'This solution uses the obscure and almost never used Range.ID 
'property, which allows the old values to persist when the workbook 
'is saved.

'At any time you can get at the old value of a cell and it will 
'indeed be different than a new current value:

With Sheet1
	MsgBox .[a1].Value
    MsgBox .ValueLast(.[a1])
End With

Example 2: excel vba what happens to range objects if user deletes cells

'Your code will raise a 'Runtime error 424: Object required'
'if a user deletes a range that your code has an open
'object reference to:

Set r = [a1]
Stop
'Now delete the first row and continue...
  
MsgBox r		'<--displays: Runtime error 424: Object required


'To avoid this problem, use the following VBA function to 
'test the validity of a range before accessing it:  

Function InvalidRangeReference(r As Range) As Boolean
    On Error Resume Next
    If r.Count = 0 Then InvalidRangeReference = Err
End Function
  
'-------------------------------------------------------------------- 
  
Set r = [a1]
Stop
'Now delete the first row and continue...
If InvalidRangeReference(r) Then Set r = [a1]

'Now you have a brand new reference to A1.
MsgBox r      	'<--works perfectly

Tags:

Vb Example