I am working on a spreadsheet that has data I need to remove based on three different cells. Here is my goal:
If A1=31 AND B1 contains a date > than the date in C1, delete the entire row. I need to loop through the full data set and check each row. I have it removing the row if A1=31 and looping through the worksheet, but can't seem to add the date component.
Here is my coding. It is probably kind of messy since mostly copied and pasted. I am not an a very experienced coder and don't really care what it looks like as long as it works. I am also not working with massive datasets so speed isn't an issue.
Sub Remove_Future_Dates()
Dim Firstrow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
Firstrow = ThisWorkbook.Worksheets(2).UsedRange.Cells(1).Row
LastRow = ThisWorkbook.Worksheets(2).Cells(ThisWorkbook.Worksheets(2).Rows.Count, "A").End(xlUp).Row
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
'.ScreenUpdating = False
End With
With ActiveSheet
.Select
Firstrow = .UsedRange.Cells(1).Row
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Lrow = LastRow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "31" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
End Sub
If you are not going to run this code on different sheets you should change ActiveSheet to the index number like you did with Worksheets(2). It is preferred to use worksheet names and best to use code names though.
You will have to define the 'OtherWorksheet' name (or index) I used and adjust its column and row. Uncomment the commented parts only after you got the code working.
I make this code working, if you provide the names or the indexes of all the worksheets envolved and an exact 'series of 3' of data eg A1, B1 on Sheet1, worksheet(1), Worksheet("Master") or ActiveSheet and C2 on Sheet3, Worksheet(3) or Worksheet("Slave").
Option Explicit
Sub Remove_Future_Dates()
Dim Firstrow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
' With Application
' CalcMode = .Calculation
' .Calculation = xlCalculationManual
' .ScreenUpdating = False
' End With
On Error GoTo SafeExit
With ThisWorkbook.Worksheets(2)
Firstrow = .UsedRange.Cells(1).Row
LastRow = .Worksheets(2).Cells(Rows.Count, "A").End(xlUp).Row
End With
With ActiveSheet
For Lrow = LastRow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "31" _
And .Offset(0, 1) > _
ThisWorkbook.Worksheets("OtherWorksheet") _
.Cells(Lrow + 1, "C") _
Then .EntireRow.Delete
End If
End With
Next Lrow
End With
SafeExit:
' With Application
' CalcMode = .Calculation
' .Calculation = xlCalculationManual
' .ScreenUpdating = True
' End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.