循环查找依赖关系

我有一张名为“Dependencies”的工作表,如下图所示。 实际数据略有不同,但这个例子将说明这一点。

在这里输入图像说明

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 

我有另一张名为“Sheet1”的表。 看起来像这样

[![在这里input图片描述] [2]] [2]

我想弄清楚如何创build一个macros来产生这样的最终结果。

[![在这里input图片描述] [3]] [3]

因此,基本上,我需要检查Sheet 1中的单元格B2中的值,在Dependencies表的ColumnA中查找所有匹配项,并在ColumnB中连接相邻单元格中的所有值。 结果必须连在一起,在每个值之间换行。 此外,我需要将Sheet1中的ID附加到最​​终结果中。

我尝试了一些像Vlookups和索引/匹配,但没有得到任何工作。 我敢肯定,VBA是可能的。 我只是不知道该怎么做。 我仍在testing一些想法。 我会回发我有任何更新。 如果有人能在我之前弄清楚,请分享你的答案。

谢谢大家!

我现在回到我的办公室,并在我的实际数据集上进行testing。 逻辑中有一个小小的缺陷,所以行号是closures的。 我需要在Dependence和TaskTitle之间进行一些查找,并取出TaskTitle的编号。 看下面的图片。

在这里输入图像说明

所以,这:3.完全公平的贷款authentication应该是这样的:1.完整的公平贷款authentication。 此外,这4更新公平借出收费信息应该是这样的:3.更新公平借出收费信息。

是否有意义?

假设代码中的工作表名称是“Sheet1”和“Dependencies”,并且数据从每个工作表的第1列开始(Sheeet1中的第3行和Dependence中的第2行),则下一个代码应该生成所需的输出

 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 

如果您有任何问题,请告诉我。 希望这对你有所帮助

尝试这个。 如果你有问题,请告诉我。

 '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