I need to go through the existing formula and delete entire row if there is no minus sign in it. Is there a way to go through the formula char by char or there is another way to identify if there is a “-” in the formula?
Sub clearvalidation()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
Set vali = ws.Cells.Find("Validation", LookIn:=xlValues, LookAt:=xlPart)
If Not vali Is Nothing Then
fr = vali.Row + 2
lr = ws.Cells(fr + 1, 6).End(xlDown).Row
For exclrow = fr To lr
For Each char In ws.Cells(exclrow, 7)
If char = "-" Then
check = "ok"
Exit For
End If
Next char
If check <> ok Then check = "delete"
Select Case check
Case Is = "ok"
Stop
Case Is = "delete"
Stop
End Select
Next exclrow
End If
vali = Empty
Next ws
End Sub
To rapidly work through each worksheet and delete rows that do not contain a hyphen in column G, the AutoFilter Method will likely work best.
Sub Macro1()
Dim w As Long
For w = 1 To Worksheets.Count
With Worksheets(w)
If .AutoFilterMode Then .AutoFilter = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=7, Criteria1:="<>*-*"
With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If Application.Subtotal(103, .Cells) Then
.Cells.EntireRow.Delete
End If
End With
End With
End With
Next w
End Sub
You should use Instr
that will return 0 if the substring your are looking for isn't found or the index of that substring if it is found.
And when you loop on rows to delete, you should go from bottom to top using Step -1
to avoid missing some rows (if the row i is deleted, the row i+1 will become the new row i and you'll miss it after the next).
Sub clearvalidation()
Dim ws As Worksheet, _
FirstAddress As String, _
cF As Range
For Each ws In ActiveWorkbook.Sheets
ws.Range("A1").Activate
With ws.Cells
Set vali = .Find("Validation", LookIn:=xlValues, LookAt:=xlPart)
If Not vali Is Nothing Then
FirstAddress = vali.Address
'If there is a result, keep looking with FindNext method
Do
fr = vali.Row + 2
lr = ws.Cells(fr + 1, 6).End(xlDown).Row
For exclrow = lr To fr Step -1
If InStr(1, ws.Cells(exclrow, 7).Formula, "-") Then
ws.Cells(exclrow, 7).EntireRow.Delete shift:=xlUp
Else
End If
Next exclrow
Set vali = .FindNext(vali)
'Look until you find again the first result
Loop While Not vali Is Nothing And vali.Address <> FirstAddress
End If
vali = Empty
End With
Next ws
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.