VBA 尝试在工作表的一部分中查找字符串是否存在(或不存在)以删除(或不删除)某些行

问题描述 投票:0回答:1

我有一个报告模板,测试软件使用它来打印报告。该软件将包括测试测量和结果,并且可以运行小型宏。

我的模板包含测试的所有可能限制,并且根据测试使用的标准,我希望它删除未使用的限制。

我正在尝试查找 3 个不同的字符串(对应于 EMC 标准标题)以查看是否需要删除特定于其中一个标准的某些行。

基本上,如果我在标准列表中有 55015,我会保留所有行,但如果我没有它并且我有 55032 或 55014,我会删除 2 行。

这是我的代码:

Sub Define_Limit()

If InStr(Range("E8:J9").Value, "*55015*") = 0 Then
If (InStr(Range("E8:J9").Value, "*55032*") = 1 Or InStr(Range("E8:J9").Value, "*55014*") = 0) Then

Cells(68, 2).EntireRow.Delete       'deleting the row with extra limits i.e. 9k-->150KHz
Cells(69, 2).EntireRow.Delete

Cells(100, 2).EntireRow.Insert
Cells(101, 2).EntireRow.Insert

End If
End If
End Sub

我也尝试过这个:

If (((CountIf("E8:J9", "*55032*"),True,False) = True) Or (CountIf("E8:J9", "*55014*"),True,False) = True) Then
If (((CountIf("E8:J9", "*55015*"),True,False) = False)) Then
 
Cells(68, 2).EntireRow.Delete       'deleting the row with extra limits i.e. 9k-->150KHz
Cells(69, 2).EntireRow.Delete
excel vba
1个回答
0
投票

将此作为可能的替代解决方案发布:

Sub Define_Limit()

Dim ws As Worksheet
Dim testRange As Range, findRange1 As Range, findRange2 As Range

Set ws = ActiveSheet
Set testRange = ws.Range("E8:J9")

With testRange

    Set findRange1 = .Find("55015", LookIn:=xlValues)
    If Not findRange Is Nothing Then Exit Sub
    
    Set findRange1 = .Find("55032", LookIn:=xlValues)
    Set findRange2 = .Find("55014", LookIn:=xlValues)
    If (Not (findRange1 Is Nothing)) And (Not (findRange2 Is Nothing)) Then
    
        Cells(69, 2).EntireRow.Delete   'deleting the row with extra limits i.e. 9k-->150KHz
        Cells(68, 2).EntireRow.Delete
        
        Cells(100, 2).EntireRow.Insert
        Cells(101, 2).EntireRow.Insert
    End If
    
End With

End Sub
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.