隐藏不包含数组不匹配单词的单元格行 VBA

Hide Rows VBA Code of Cells Not Containing Words From Array Mismatch

基本上我的 vba 代码运行得非常好。但是当我尝试将它复制到另一个命令按钮时,只是更改 sheet 名称和子名称,它不起作用并且显示类型不匹配。这是正确运行的 VBA 代码。

    Sub hide_Rows_by_cell_value()
    Dim wb As Workbook, CompInfo As Worksheet, MufgClient As Worksheet
    Dim srcCl As Range, lr As Long, FltCol As Range, cl As Range, hideRng As Range
    Set wb = ThisWorkbook
    Set CompInfo = wb.Sheets("Company Information")
    Set MufgClient = wb.Sheets("MUFG Client")
    
    Set srcCl = CompInfo.Cells(18, 9)
    arr = Split(srcCl.Value, ",")
    
    lr = MufgClient.Range("AC" & MufgClient.Rows.Count).End(xlUp).Row
    Set FltCol = MufgClient.Range("AC3:AC" & lr) '2nd Row contains table headers
    
    For Each cl In FltCol
        chk = 0
        For i = 0 To UBound(arr)
        chk = chk + InStr(1, cl.Value, Trim(arr(i)), vbTextCompare)
        Next
        If chk = 0 Then
            If hideRng Is Nothing Then
            Set hideRng = cl
            Else
            Set hideRng = Union(hideRng, cl)
            End If
        End If
    Next
    
    hideRng.EntireRow.Hidden = True
    
    End Sub

这是 VBA 表示类型不匹配的代码

  Sub hide_rows_by_cell_value2()
  Dim wb As Workbook, MUFGInfo As Worksheet, LendingFunding As Worksheet
  Dim srcCl As Range, lr As Long, FltCol As Range, cl As Range, hideRng As Range
  Set wb = ThisWorkbook
  Set MUFGInfo = wb.Sheets("MUFG Information")
  Set LendingFunding = wb.Sheets("Lending & Funding")

  Set srcCl = MUFGInfo.Cells(18, 9)
  arr = Split(srcCl.Value, ",")

  lr = LendingFunding.Range("AC" & LendingFunding.Rows.Count).End(xlUp).Row
  Set FltCol = LendingFunding.Range("AC3:AC" & lr) '2nd Row Contains table headers

  For Each cl In FltCol
    chk = 0
    For i = 0 To UBound(arr)
    chk = chk + InStr(1, cl.Value, Trim(arr(i)), vbTextCompare)
    Next
    If chk = 0 Then
        If hideRng Is Nothing Then
        Set hideRng = cl
        Else
        Set hideRng = Union(hideRng, cl)
        End If
    End If

  Next

  hideRng.EntireRow.Hidden = True

  End Sub

不匹配在

chk = chk + InStr(1, cl.Value, Trim(arr(i)), vbTextCompare)

我已经检查了单元格,它仍然是正确的 18,9(第 18 行第 i 列)。我还使范围与第一个 vba 代码相同,因为 sheet 内容是前一个 sheet

的复制粘贴

如有任何帮助,我们将不胜感激。

在此先感谢您。

从评论来看,您检查的范围似乎有误(“AC”列)

您可以通过在循环内的代码周围添加错误检查来掩盖检查周期中的错误:

  For Each cl In FltCol
    If Not IsError(cl) Then
      chk = 0
      For i = 0 To UBound(arr)
        chk = chk + InStr(1, cl.Value, Trim(arr(i)), vbTextCompare)
      Next
      If chk = 0 Then
        If hideRng Is Nothing Then
          Set hideRng = cl
        Else
          Set hideRng = Union(hideRng, cl)
        End If
      End If
    End If

  Next

如果您的 sheet 中的数据不包含 #N/A,则上面的代码有效,因为错误在 cl.Value 中,错误 = 2042。替换所有 #N/A(s) 只有 0。