2017-01-26 7 views
0

私は、下の画像のように、「依存関係」という名前のシートにデータを持っています。実際のデータはわずかに異なりますが、この例ではその点を説明します。依存関係を見つけるループ

enter image description here

Here is the data: 
    Category Name Dependencies 
    Beverages SoftDrinks 
    Beverages Coffees 
    Beverages Teas 
    Beverages Beers 
    Beverages Andales 
    Condiments Sweetandsavorysauces 
    Condiments Relishes 
    Condiments Spreads 
    Condiments Andseasonings 

私は古典的に 'シート1' という名前の別のシートを持っています。それはこのように見えます。

[![ここに画像の説明を入力します] [2] [2]

私はこのような最終的な結果を生成するマクロを作成する方法を把握しようとしています。

[![画像の説明を入力します。ここ] [3] [3]

だから、基本的に、私は、シート1でセルB2の値をチェックする依存シートのColumnA内のすべての一致を検索する必要があり、 ColumnB内の隣接するセルのすべての値を連結します。結果を連結し、各値の間に改行を付ける必要があります。また、最終結果にSheet1のIDを追加する必要があります。

VlookupsやIndex/Matchなどのいくつかのことを試しましたが、何もできませんでした。私はVBAで可能だと確信しています。私はちょうどそれを行う方法を正確に知らない。私はまだいくつかのアイデアをテストしています。私は私が持っているすべてのアップデートをポストバックします。私がする前に誰かがこれを理解することができれば、答えを教えてください。

ありがとうございます!

私のオフィスに戻ってきて、これを実際のデータセットでテストします。 ロジックにわずかな欠陥があり、行番号がオフです。 DependenceとTaskTitleの間で何らかのルックアップを行い、TaskTitleの番号を取得する必要があります。下の画像を見てください。

enter image description here

ので、この:3コンプリートフェア貸出証明は、このする必要があります。1.フェア貸出証明書を完了します。また、この4.公正な貸し出しの料金料金情報を更新する必要があります。3.公正な貸出料金の料金情報を更新してください。

意味がありますか?

+0

マクロは出力全体(カテゴリIDとカテゴリ名)をプロットするか、依存関係をプロットするだけですか? – Hexxed

+0

ColCの依存関係のみ。今、私はColAとColBのSheet1にいくつかのデータを持っています。上記で概説したルールに従って、ColCを入力する必要があります。 – ryguy72

答えて

1

コード内のシート名が "Sheet1"と "Dependencies"であり、データが各シートの第1列(sheeet1のRow3とDependencesのRow2)から始まると仮定すると、次のコードは出力あなたが欲しい場合は、私に教えてください。

Sub solve() 
    Dim i As Long, j As Long 
    Dim searchVal As String, ID As String, findVal As String, dependence As String, resultVal As String 

    i = 3 
    'The first While will loop all values present in colum 2 of Sheet1' 
    Do While (Sheet1.Cells(i, 2).Value <> "") 
     searchVal = Sheet1.Cells(i, 2).Value 'The value you need match in Dependences' 
     resultVal = "" 'the value you need to calculate' 
     j = 2 'also need to reset the index of second loop' 
     ID = Sheet1.Cells(i, 1).Value 'The id present in column 1' 

     'The second While loops the Dependences Sheet' 
     Debug.Print "FirstLoop: " & searchVal 
     Do While (Sheets("Dependencies").Cells(j, 1).Value <> "") 
     findVal = Dependences.Cells(j, 1).Value 
     dependence = Dependences.Cells(j, 2).Value 

     Debug.Print "key: " & findVal & ", dependence: " & dependence 
     If (searchVal = findVal) Then 'if you find a coincidence, you will modify the result string' 
      resultVal = resultVal & ID & ". " & dependence & Chr(10) 
     End If 

     j = j + 1 
     Loop 

     'finally, we writhe the result string in olumn 3' 
     Sheet1.Cells(i, 3).Value = resultVal 
     i = i + 1 
    Loop 

End Sub 

私に教えてください。これがあなたのお役に立てば幸いです

+0

完璧に動作します!本当にありがとう!!私はちょうど微調整を行いました... Do While(Sheets( "Dependencies")。セル(j、1).Value <> "") Sheet1は文字通り最初のシート上で操作を行っていたと思います。 – ryguy72

1

これを試してみてください。あなたに質問がある場合はお知らせください。

'Place Macro on Dependency Sheet 
Public Sub genMain() 
    Dim ws As Worksheet 
    Dim dbArray As Variant 
    Set ws = Worksheets(Me.Name) 

    'gets list on Dependencies stores to Array 
    dbArray = ws.Range("A2:B2", ws.Range("A2:B2").End(xlDown)) 
    Set ws = Nothing 

    'plots to sheet1 
    Set ws = Worksheets(Sheet1.Name) 
    With ws 
     Dim TotalRows As Long 
     Dim LastRow As Long 
     Dim LastCol As Long 
     ws.Select 
     LastRow = .Range("A" & .Rows.Count).End(xlUp).Row 
     LastCol = .Range("A1").CurrentRegion.Columns.Count 
     .Range("A2").Resize(LastRow - 1, LastCol).Select 
     TotalRows = Selection.Rows.Count + 1 
     Dim temp As String 

     For Row = 2 To TotalRows Step 1 
      temp = "" 
      For col = 1 To LastCol Step 1 
       If .Cells(1, col).Value Like "*Dependency*" Then 'scans all columns 
        For i = 1 To UBound(dbArray) 'searches the dbArray 
         If dbArray(i, 1) = .Cells(Row, col - 1).Value Then 'matches current Category Name with the dbArray current selection 
          If temp = "" Then 
           temp = dbArray(i, 2) ' if cell is empty 
          Else 
           temp = temp & vbCrLf & dbArray(i, 2) 'enter new line and enter dependency 
          End If 
         End If 
        Next i 
       End If 
      Next col 
      .Cells(Row, col - 1).Value = temp 
     Next Row 
    End With 
End Sub 
+0

それは私が望むことをしなかった、Hexxed。しかし、フェルナンドCTが投稿したスクリプトは完璧だったので、今はすべて設定されています。みんな、ありがとう。 – ryguy72

+0

@Fernando CT、私はちょうど論理に欠陥を見つけました。私のOPを見てください。私は今直面した問題を説明するためにそれを修正しました。 – ryguy72

+0

私はそれを理解しました。すべてセットされました。フェルナンドCTをありがとう! – ryguy72

関連する問題