我正在寻找有关如何为以下内容编写宏的帮助:
我有一张数据,包含项目名称列表(A列)。这些项目中的每一项都有与之相关的单独成本(B列),每个项目都链接到一个项目(C列)。我将所有这些数据存在于工作簿1中,工作表被称为"项目"。在一个单独的工作簿(2,Sheet," Projects")中,我列出了所有项目(A列)及其客户(B栏)。
我正在尝试编写一个能够"瀑布"项目数据(即客户),以便与特定项目相关联的任何项目将获取与该项目相关的所有其他数据 - 客户。
我想要实现的数据输出示例如下表所示,其中客户数据是" waterfalled"匹配相应的项目。
我在Excel中有一个逻辑功能,可以将相应的客户分配给项目,但这已固定到某些列。数据列数将改变,因此我需要一个宏,它将根据列标题分配值。
Original data sheet 1:
Project Customer
1 John
1 John
1 John
2 Mike
2 Mike
2 Mike
3 Sam
Original data Sheet 2:
Name Cost Project
A 25 1
B 30 1
C 40 1
D 15 2
E 20 2
F 30 2
G 45 3
Example of expected output:
Name Cost Project Customer
A 25 1 John
B 30 1 John
C 40 1 John
D 15 2 Mike
E 20 2 Mike
F 30 2 Mike
G 45 3 Sam
我已经开始编写宏了,但我对此很新,我觉得这对我的宏写作水平来说有点先进!任何帮助将不胜感激!!
Sub ProjectWaterfall()
Dim sourceWS As Worksheet, targetWS As Worksheet
Dim count As Range, matchingCell As Long
Dim RangeInSheet1 As Variant
Dim RangeInSheet2 As Variant
Dim lastCol As Long, lastRow As Long, srcRow As Range
Dim found1 As Range, found2 As Range
Application.ScreenUpdating = False
Set sourceWS = Workbooks("Workbook1.xlsm").Worksheets("Project") 'Needs to be open
Set targetWS = Workbooks("Workbook1.xlsm").Worksheets("Technologies") 'Needs to be open
For Each count In sourceWS.Range("B2", sourceWS.Range("B" & Rows.count).End(xlUp))
matchingCell = 0
On Error Resume Next
matchingCell = Application.Match(count, targetWS.Columns(1), 0)
On Error GoTo 0
If matchingCell <> 0 Then targetWS.Range("W" & matchingCell).Value = count.Offset(, 1)
Next count
Application.ScreenUpdating = True
End Sub