我是VBA的新手,并且遇到了我想要解决的问题。我有一张表,我称之为静态数据(Sheet1)。它具有客户名称,客户ID和用于识别用例的列。我的flex数据(Sheet2)有客户ID,用例和状态。我试图提出VBA代码,将每个客户的状态复制到相应的用例列/单元格中。 Sheet2中无法与Sheet 1中的客户匹配的任何数据都应复制到单独的工作表中 任何帮助将不胜感激。
以下是如何组装纸张
表1静态数据
Customer Name | Customer ID | Case 1 | Case 2 | Case 3 | Case 4 | Case 5
------------------------------------------------------------------------
Customer A | 111 | | | | |
Customer B | 222 | | | | |
Customer C | 333 | | | | |
Customer D | 444 | | | | |
Customer E | 555 | | | | |
工作表2 Flex数据
Customer ID | Use Case | Status
---------------------------------
111 |Case 1 | Forecast
222 |Case 1 | Upside
111 |Case 2 | Upside
333 |Case 3 | Pipeline
444 |Case 4 | Pipeline
222 |Case 4 | Forecast
666 |Case 5 | Pipeline
输出表或表1
Customer Name | Customer ID | Case 1 | Case 2 | Case 3 | Case 4 | Case 5
------------------------------------------------------------------------
Customer A | 111 |Forecast|Upside | | |
Customer B | 222 |Upside | | |Forecast|
Customer C | 333 | | |Pipeline| |
Customer D | 444 | | | |Pipeline|
Customer E | 555 | | | | |
答案 0 :(得分:1)
您可以使用多标准索引/匹配:
=Index([Status Range],Match([customer ID]&[Case No.],[customer ID Range]&[Case No. Range],0)
作为数组公式输入, CTRL + SHIFT + ENTER
然后,最后环绕=IfError([index/match],"")
以隐藏任何内容。
所以你只是在一个单独的页面上引用数据,我只是把它放在同一页面上以便于展示。
答案 1 :(得分:0)
好的,看看我们是否可以通过VBA完成这项工作。 这是VBA的潜在解决方案。这很快又脏,但它完成了工作。这取决于sheet1和Sheet2。
MATHEMATICAL MONOSPACE SMALL U
答案 2 :(得分:0)
Sub main()
Dim cell1 As Range, cell2 As Range, flexRng As Range, filteredRng As Range, headersRng As Range
With Worksheets("Sheet 2")
Set flexRng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With
With Worksheets("Sheet 1")
Set headersRng = .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))
For Each cell1 In .Range("B2", .Cells(.Rows.Count, 2).End(xlUp))
If GetFilteredRange(flexRng, cell1.Value, filteredRng) Then
For Each cell2 In filteredRng
.Cells(cell1.Row, headersRng.Find(what:=cell2.Offset(, 1).Value, LookIn:=xlValues, lookat:=xlWhole).Column).Value = cell2.Offset(, 2)
Next
End If
Next
End With
End Sub
Function GetFilteredRange(rangeToFilter As Range, filterValue As Variant, filteredRange As Range) As Boolean
With rangeToFilter
.AutoFilter Field:=1, Criteria1:=filterValue
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then
GetFilteredRange = True
Set filteredRange = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
End If
.Parent.AutoFilterMode = False
End With
End Function