我在名为'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'的表格。它看起来像这样。
[![在此处输入图像说明] [2]] [2]
我正在试图弄清楚如何创建一个宏来产生这样的最终结果。
[![在此处输入图像说明] [3]] [3]
因此,基本上,我需要在Sheet 1中检查Cell B2中的值,在Dependencies表的ColumnA中找到所有匹配项,并在ColumnB中连接相邻单元格中的所有值。结果必须连接在一起,每个值之间都有换行符。另外,我需要将Sheet1中的ID附加到最终结果中。
我尝试了一些像Vlookups和Index / Match这样的东西,但是无法正常工作。我相信VBA是可能的。我只是不知道该怎么做。我还在测试一些想法。我会回复我的任何更新。如果有人能在我做之前弄清楚这一点,请分享你的答案。
感谢所有人!
我现在回到我的办公室,并在我的实际数据集上进行测试。 逻辑中存在轻微缺陷,因此行号已关闭。我需要在Dependence和TaskTitle之间进行某种查找,并获取TaskTitle的编号。看下面的图片。
所以,这个:3。完整的公平借贷证明应该是这样的:1。完成公平借贷证明。此外,这4更新公平贷款收费站信息应该是这样的:3。更新公平贷款收费站信息。
有意义吗?
答案 0 :(得分:1)
假设代码中的工作表名称是“Sheet1”和“Dependencies”,并且数据从每个工作表的第1列开始(Sheeet1中的第3行和Dependences中的第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
如果您有任何疑问,请告诉我。希望这能帮到你
答案 1 :(得分: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