简体   繁体   English

通过公式Excel VBA循环

[英]Loop through formula Excel VBA

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? 是否可以通过char逐个遍历char公式,或者有另一种方法可以识别公式中是否存在“-”?

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. 若要快速浏览每个工作表并删除G列中不包含连字符的行,“自动筛选方法”可能会最好地工作。

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. 您应该使用Instr ,如果找不到要查找的子字符串,则返回0;如果找到该子字符串的索引 ,则返回0。

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). 并且,当您循环删除行时,应使用Step -1从下至上进行操作,以免丢失某些行(如果删除了第i行,则第i + 1行将成为新的第i行,而您会错过它下一个)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM