Vba - 包含字符的匹配命令

时间:2017-09-01 11:10:50

标签: excel vba excel-vba

我有以下vba代码:

Function countX(area_x As String, area_w As String)

Dim Nr As Integer, count_x As Integer, Col_Ind
Dim temp_area As Range

For Nr = 10 To 1 Step -1

With Worksheets(CStr(Nr))

    If WorksheetFunction.CountIf(.Range(area_w), "*w*") > 0 Then

        'Find "w" in area_w
        Col_Ind = WorksheetFunction.Match("w", .Range(area_w))

        MsgBox (Col_Ind)

        'Reset numbers of "x" in area_w
        Set temp_area = .Range(.Cells(9, 4).Offset(0, Col_Ind), .Cells(9, 9))
        count_x = count_x + WorksheetFunction.CountIf(temp_area, "*x*")
        Exit For
    Else

        count_x = count_x + WorksheetFunction.CountIf(.Range(area_x), "*x*")

    End If

End With

Next
countX = count_x
End Function

我有10张纸 - 都具有相同的结构。

  • D9-I9 =>用户shell设置“x”(如果需要)
  • K9-P9 =>用户Shell设置“w”(如果需要)

我有另一张表(相同的工作表)作为概述。在模具页面上,我希望看到所有10张纸中的“x”。

困难的部分是:

  • 在设定的“w”之后只计算“x”。

实施例: 在表8中设置了“w”。 现在应该只计算从第8页(在w之后)到10的“x”。

图片示例:

enter image description here

计数“x”的结果将为“2”,因为在设定的“w”之后有2“x”

这就是问题所在。 它只有在用户设置“w”时才有效。但是“w / r”或“r / w”等不起作用。

为此我需要一个解决方案:)

2 个答案:

答案 0 :(得分:1)

也许不是最好的解决方案(我现在没有时间自己尝试),但这样做的一种方法是循环使用InStr(cell,“w”)>每个单元格都有0 ...

答案 1 :(得分:1)

只有3种方法可以做到:

Public Function countW(area_w As String)
  With Application.Caller.Parent.Range(area_w)
    Set countW = .Find("w*", .Cells(1, 1), xlValues, xlWhole, , xlNext, 0)
    If .Rows.Count - 1 Then countW = countW.Row - .Row + 1 Else countW = countW.Column - .Column + 1
  End With
End Function

Public Function countW(area_w As String)
  With Application.Caller.Parent.Range(area_w)
    countW = Application.Match("w", Application.Transpose(.Parent.Evaluate("INDEX(LEFT(" & area_w & "),)")), 0)
  End With
End Function

Public Function countW(area_w As String)
  countW = Application.Match("w*", Application.Caller.Parent.Range(area_w), 0)
End Function

修改
在我看来,你的“变化”看起来更像是一个新问题,而不是一个小的更新(也应该被问为一个新的)。我这次仍然会给你一个解决方案。

Public Function countX(area_x As String, area_w As String) As Long
  Dim i As Long, to_offset As Variant, x_rng As Range
  For i = 1 To 10 'upper loop for worksheets
    With Worksheets(i)
      to_offset = Application.Match("*w*", .Range(area_w), 0)
      If IsNumeric(to_offset) Then 'run only if there is a "w" found
        If .Range(area_x).Columns.Count > 1 Then 'just one row
          Set x_rng = Intersect(.Range(area_x), .Range(area_x).Offset(0, to_offset))
        Else 'just one column
          Set x_rng = Intersect(.Range(area_x), .Range(area_x).Offset(to_offset))
        End If
        If .Range(area_x).Cells.Count > to_offset Then 'not running if the "last" cell has the was as we cout only "after" it
          countX = countX + Application.CountIf(x_rng, "*x*")
        End If
      End If
    End With
  Next
End Function

如果按照查找所有工作表的方式进行操作,此代码也会执行:

Public Function countX(area_x As String, area_w As String) As Long
  Dim ws
  For Each ws In ThisWorkbook.Worksheets
    If IsNumeric(Application.Match("*w*", Intersect(ws.Range(area_w), ws.Range(area_w).Offset(, -1)), 0)) Then
      countX = countX + Application.CountIf(Intersect(ws.Range(area_x), ws.Range(area_x).Offset(0, Application.Match("*w*", ws.Range(area_w), 0))), "*x*")
    End If
  Next
End Function