我有一个包含3个工作表的excel文件:(1)报告,(2)类别-1和(3)类别-2。
“报告”工作表包含类别1和类别2数据的摘要信息,其中包含每个类别和分组值的唯一ID。 See attached example image
我需要将“Category1”工作表的“Category1SubId”列中的唯一值复制到“column-D”中的“Report”工作表,然后将“Category1Value”的相关值从“Category1”工作表复制到“Report”工作表中“COLUMN-E”。
例如,公式/ VBA代码将Apple的1,2,3,4,5中的值复制到“Category1”工作表中的“Report”工作表中,然后将“Breakfast”值复制为“Apple和1”的组合“从第1类工作表中进入报告工作表的单元格E2,依此类推。
我尝试了INDEX,VLOOKUP,ROWS和SEARCH公式的组合,但它们都没有帮助。我也尝试过VBA代码,但它没有用。
任何人都可以通过公式或VBA代码帮助解决这个难题吗?
此工作表的示例为attached here。
我尝试过关注VBA代码
Sub CopyData()
Dim category1LookupValueRange As Range
Dim category2LookupValueRange As Range
Dim category1SourceRange As Range
Dim category2SourceRange As Range
Dim category1TargetRange As Range
Dim category2TargetRange As Range
Dim targetValue As String
Set category1LookupValueRange = Worksheets("Report").Range("C2:C30")
Set category2LookupValueRange = Worksheets("Report").Range("F2:F30")
Set category1SourceRange = Worksheets("Category1").Range("A2:C30")
Set category2SourceRange = Worksheets("Category2").Range("A2:C30")
Set category1TargetRange = Worksheets("Report").Range("D2:E30")
Set category2TargetRange = Worksheets("Report").Range("G2:H30")
Dim iRow As Integer
Dim category1LookupValue As Object
Dim category1Id As Object
Dim category1IdValue As Object
Dim category2LookupValue As Object
Dim category2Id As Object
Dim category2IdValue As Object
For iRow = 1 To category1LookupValueRange.Count
Set category1LookupValue = category1LookupValueRange.Cells(iRow, 1)
Set category2LookupValue = category2LookupValueRange.Cells(iRow, 1)
Set category1Id = category1SourceRange.Range.Cells(category1SourceRange.Find(category1LookupValue, LookAt:=xlWhole), 1)
Set category2Id = category2SourceRange.Range.Cells(category2SourceRange.Find(category2LookupValue, LookAt:=xlWhole), 1)
Set category1IdValue = category1SourceRange.Range.Cells(category1SourceRange.Find(category1LookupValue, LookAt:=xlWhole), 1)
Set category2IdValue = category2SourceRange.Range.Cells(category2SourceRange.Find(category2LookupValue, LookAt:=xlWhole), 1)
If Not (category1LookupValue Is Nothing) Then
If (category1SourceRange.Cells(iRow, 1) = category1LookupValue) Then
Set category1TargetRange.Range.Cells(iRow, 1) = category1Id
Set category2TargetRange.Range.Cells(iRow, 1) = category2Id
End If
End If
Next iRow
在报表工作表
中的单元格区域D2到D30中的数组公式{= IF(ISERROR(SMALL(IF(IF(ISERROR(SEARCH($ C2,组别$ A $ 2:$ A $ 30)),FALSE,TRUE),ROW(组别$ B $ 1:$ B $ 30 )),ROW(组别$ B $ 1:$ B $ 30))), “”,INDEX(组别$ A $ 2:$ C $ 100中,SMALL(IF(IF(ISERROR(SEARCH($ C2,组别$ A! $ 1:!$ A $ 30)),FALSE,TRUE),ROW(组别$ B $ 1:$ B $ 30)),ROW(组别$ B $ 1:$ B $ 100)),1))}