2017-09-13 10 views
0

Excel VBAで検索+合計エンジンをコーディングしようとしています。私はエントリーのリストを持っていますが、コードには特定のタイプのコスト(たとえば "1 Equipment")を検索してから、すべてのEquipment Costsを合計して別のワークシートのセルに印刷する必要があります。相続人は、私がこれまでに入力した内容:Excel VBA検索+合計エンジン

Sub Sample() 
Dim fnd As String 
Dim MyAr 
Dim i As Long 
Dim rng As Range, FoundCell As Range, LastCell As Range, myRange As Range 

Set myRange = ActiveSheet.UsedRange 
Set LastCell = myRange.Cells(myRange.Cells.Count) 

fnd = "1 Equipment" 

MyAr = Split(fnd, "/") 

For i = LBound(MyAr) To UBound(MyAr) 

    Set FoundCell = myRange.Find(what:=MyAr(i), after:=LastCell) 

    If Not FoundCell Is Nothing Then 
     FirstFound = FoundCell.Address 
    End If 
Set rng = FoundCell 
     Do Until FoundCell Is Nothing 
      Set FoundCell = myRange.FindNext(after:=FoundCell) 
       Set rng = Union(rng, FoundCell) 
      If FoundCell.Address = FirstFound Then Exit Do 
     Loop 

If Not rng Is Nothing Then 
rng.**(____in here it needs to sum the value of the cell in 
the column to the right of where the word was found___)** 

End If 

    Next i 

End Sub 

これは、値(私はタイプされたいくつかの値を持っていますが、エンジンはすべての方法ワークシートの最後に行かなければならない)の私のリストの写真です: https://i.stack.imgur.com/ZPIc9.png "1つの機器"の値を合計し、その合計を別のセルに表示する必要があるので、この回答は11750です。

私はVBAの初心者ですので、本当に助けが必要です。ありがとうございました!

私はカスタム関数のようなより多くのあなたが与えられた値に対応するすべてのセルを見つけて、あなたは、隣接する細胞数を合計シートに追加することだろう
+0

SUMIFS()では不十分ですか? –

+0

なぜあなたは 'fnd =" 1 Equipment "を"/"要素で分割して配列を作成していますか?あなたが探しているものは 'fnd'なので' .Find(what:= fnd、after:= LastCell) ' – danieltakeshi

+0

SUMIFSでやったことがあります。ありがとうございました! –

答えて

0

:のように使用するために

Public Function SumInvestments(ByVal InvType As String) As Long 

    Dim allCells As Range 
    Dim singleCell As Range 
    Dim totalSum As Long 

    Set allCells = Range("A1:A" & Range("A1").End(xlDown).Row).Find(InvType, lookAt:=xlWhole) 
    If Not allCells Is Nothing Then 
     For Each singleCell In allCells 
      totalSum = totalSum + singleCell.Offset(0,1).Value 
     Next singleCell 
    End If 
    SumInvestments = totalSum 

End Function 

これは

=SumInvestments("1 Equipment") 
+0

Excelの組み込み関数であるSUMIFS()もやっていると思います。 –

+0

ええ、私はそれをSUMIFS()で終わらせました。しかし、あなたの答えは本当にありがとう! –