2017-07-12 4 views
0

私は次のような状況の解決法が必要です: 私は4枚のExelファイルを持っています。 すべてのシートの構造は同じですが、内容のみが異なります。vbaの値 "x"を数えよう

説明:

  • ユーザーがエリアD9に「X」を設定する必要があります。I9(この「x」は、すべてのシートにカウントする必要があるが、特別な事情がある:
  • は、

    :のみすべての「X」「Y」領域の後に、このシートの

    例を数えるべきである場合にはP9

:ユーザがエリアK9で「Y」を設定することができ

シート1:D9:I9( "X")= 6が "X" enter image description here

シート2:D9:I9( "X")= 6 enter image description here

シート3を "×": D9:I9( "x")= 6 "x" | M9( "y") enter image description here

そして、シート3には特別な状況があります。 ユーザーは6 "x"を設定していますが、 "y"も設定しています。

これは、「y」のような同じ領域の前に設定されている「x」は数えてはならないことを意味します。

私は、次のVBAコードを持っている「X」は私のすべてを数えるwhichsではなく、この「特別な状況」(私は方法がわからない)コードの下

Function breaking_count() 

Const Area_x As String = "$D$9:$I$9" 
Const Area_y As String = "$K$9:$P$9" 

Dim Nr As Integer, Count_x As Integer 


For Nr = 4 To 1 Step -1 

    With Worksheets(CStr(Nr)) 

     If WorksheetFunction.CountIf(.Range(Area_y), "*y*") > 0 Then Exit For 
     Count_x = Count_x + WorksheetFunction.CountIf(.Range(Area_x), "*x*") 

    End With 

Next 
End Function 

答えて

0

メッセージボックスが表示されますウィットカウント数がxの各シートについて、

Sub breaking_count() 
    Const Area_x As String = "$D$9:$I$9" 
    Const Area_y As String = "$K$9:$P$9" 
    Dim Nr As Integer, Count_x As Integer, Col_Ind 
    Dim temp_Area As Range 

    For Nr = 4 To 1 Step -1 
     'With Worksheets(CStr(Nr)) 
     With Worksheets(Nr) 
      If WorksheetFunction.CountIf(.Range(Area_y), "*y*") > 0 Then 
       'find the index of "y" in Area_y 
       Col_Ind = WorksheetFunction.Match("y", .Range(Area_y)) 
       'Reset the range Area_y in temp_Area using offset 
       Set temp_Area = .Range(.Cells(9, 4).Offset(0, Col_Ind), .Cells(9, 9)) 
       Count_x = Count_x + WorksheetFunction.CountIf(temp_Area, "*x*") 
      Else 
       Count_x = Count_x + WorksheetFunction.CountIf(.Range(Area_x), "*x*") 
      End If 
      MsgBox Worksheets(Nr).Name & " : " & Count_x 
      Count_x = 0 
     End With 

    Next 
End Sub 

明らかでないことがあれば教えてください。

+0

ありがとうございました。 – Ghost108

+0

@ Ghost108 - ようこそ! – Mrig

関連する問題