【问题标题】:Skip iteration of loop under one condition在一种条件下跳过循环迭代
【发布时间】:2020-06-21 23:19:04
【问题描述】:

我是 VBA 新手,所以提前感谢您的耐心等待。我创建了一个 sub 来检查 col F 中的每个值是否在另一张纸的 col A 中找到。如果该值不在另一张纸上,则会以黄色突出显示单元格。有一个问题:如果单元格包含“XXX”,我希望循环不突出显示单元格并继续下一个。我发现了许多类似的questions,并且许多警告不要使用GoTo 作为解决方法。这是我的代码:

'PURPOSE: Finds and highlight all values in Routing List Adapters that are not found in Summary Adapters
Sub SummaryCheck_1()
Dim RoutingList As Worksheet
Dim Summary As Worksheet
Dim RoutingList_Adapters1 As Range
Dim Summary_Adapters As Range
Dim Adapter As Range
Dim AdapterValue As String
Dim Match As Range

'Sets worksheets
Set RoutingList = ActiveWorkbook.Sheets("Routing List")
Set Summary = ActiveWorkbook.Sheets("Summary")


'Sets Routing List Adapter Fitting (1) range and Summary Adapter range
'Corresponds to Adapter Fitting (1) column
Set RoutingList_Adapters1 = RoutingList.Range(("F3"), RoutingList.Range("F3").End(xlDown))
Set Summary_Adapters = Summary.Range(("A4"), Summary.Range("A4").End(xlDown))

'Finds and highlights all Routing List Adapter Fittings (1) not found in Summary Adapters
For Each Adapter In RoutingList_Adapters1
    AdapterValue = Adapter.Value
    Set Match = Summary_Adapters.Find(What:=AdapterValue, LookAt:=xlWhole)
    If Match Is Nothing Then
        Adapter.Interior.Color = RGB(255, 255, 0)
    ElseIf Not Match Is Nothing Then
        Match.Interior.Color = RGB(0, 255, 0)
    ElseIf InStr(AdapterValue, "XXX") > 0 Then
        'Here is where I want to insert code to skip cells containing "XXX"
    End If
Next Adapter
End Sub

如果这个潜艇需要更多解释,请告诉我。我尝试了一种解决方法,即将包含“XXX”的所有单元格的内部颜色重置为RGB(255,255,255),但不幸的是,许多工作表行已经进行了颜色编码,所以我需要完全跳过包含“XXX”的单元格。提前感谢您提供有关如何跳过此单个条件的循环迭代的任何见解!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    有更好的方法,可以快速的 Match.Interior.Color = Match.Interior.Color

    【讨论】:

    • 我在我写'Here is where I want to insert code to skip cells containing "XXX"的代码中添加了Match.Interior.Color = Match.Interior.Color,但它仍然使XXX列变黄。我是否需要更改拥有Match.Interior.Color = Match.Interior.Color 代码的位置?也许在 ElseIf 中不是正确的位置?
    【解决方案2】:

    添加And InStr(AdapterValue, "XXX") = 0,将代码改为:

    'PURPOSE: Finds and highlight all values in Routing List Adapters that are not found in Summary Adapters
    Sub SummaryCheck_1()
    Dim RoutingList As Worksheet
    Dim Summary As Worksheet
    Dim RoutingList_Adapters1 As Range
    Dim Summary_Adapters As Range
    Dim Adapter As Range
    Dim AdapterValue As String
    Dim Match As Range
    
    'Sets worksheets
    Set RoutingList = ActiveWorkbook.Sheets("Routing List")
    Set Summary = ActiveWorkbook.Sheets("Summary")
    
    
    'Sets Routing List Adapter Fitting (1) range and Summary Adapter range
    'Corresponds to Adapter Fitting (1) column
    Set RoutingList_Adapters1 = RoutingList.Range(("F3"), RoutingList.Range("F3").End(xlDown))
    Set Summary_Adapters = Summary.Range(("A4"), Summary.Range("A4").End(xlDown))
    
    'Finds and highlights all Routing List Adapter Fittings (1) not found in Summary Adapters
    For Each Adapter In RoutingList_Adapters1
        AdapterValue = Adapter.Value
        Set Match = Summary_Adapters.Find(What:=AdapterValue, LookAt:=xlWhole)
        If Match Is Nothing And InStr(AdapterValue, "XXX") = 0 Then
            Adapter.Interior.Color = RGB(255, 255, 0)
        ElseIf Not Match Is Nothing Then
            Match.Interior.Color = RGB(0, 255, 0)
        End If
    Next Adapter
    End Sub
    

    【讨论】:

      【解决方案3】:

      为什么不完全跳过 XXX 个?

          If InStr(AdapterValue, "XXX") = 0 Then
              If Match Is Nothing Then
                  Adapter.Interior.Color = RGB(255, 255, 0)
              Else
                  Adapter.Interior.Color = RGB(0, 255, 0)
              End If
          End If
      

      我假设您想将找到匹配项的单元格涂成绿色,也许我误解了您的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 2021-11-15
        • 2019-05-17
        • 2015-08-16
        相关资源
        最近更新 更多