简体   繁体   English

Excel VBA更新工作簿中的链接到相同的目标

[英]Excel VBA update links in workbook to same destination

My issue: 我的问题:

I have a sheet, where I compare two sets of data. 我有一张工作表,在这里我比较两组数据。 These two sets of data, are might have some with the same description number... 这两组数据可能有一些具有相同的描述编号...

In this case I want them to merge, but at the moment I do it manually.. 在这种情况下,我希望它们合并,但是现在我手动进行。

My data looks like in both ranges: 我的数据看起来在两个范围内:

Date        Description  Amount  
15-12-2017  V576954    -1.289,89  
15-12-2017  V576954    1.289,89  
15-12-2017  V576954    -1.896,45  
19-12-2017  V586894    -789,52  
28-12-2017  GS5155692  -9.286,00  
28-12-2017  GS5155692  9.286,00  
28-12-2017  GS5155692  -11.857,50  

GOAL: 目标:

Is there somehow, that I can pull out the Functions a Pivot Table is using, and sum up the values with the same description?? 是否可以以某种方式提取出数据透视表正在使用的功能,并用相同的描述总结这些值?
I don't want to get a Pivot Table out of my data, I want to merge the values of the values, which has the same description, along as having the same Date, and the same description :) 我不想从数据中获取数据透视表,而是要合并具有相同描述,相同日期和相同描述的值的值:)

If I understand your question correctly, you want to sum the values on Column C if the data in Column A and B match, then the following would do that for you: 如果我正确理解了您的问题,那么如果列A和列B中的数据匹配,则希望对列C中的值求和,那么下面的操作将为您完成:

Sub foo()
Sheets("Sheet1").Copy After:=Sheets("Sheet1")
ActiveSheet.Name = "Summary"
Dim ws As Worksheet: Set ws = Sheets("Summary")
'declare and set your worksheet, change Sheet1 to the sheet you are using
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'find the last row with data on Column A
    For i = 2 To LastRow 'loop from row 2 to last
        ws.Cells(i, 4).FormulaR1C1 = "=SUMIFS(C[-1],C[-2],RC[-2],C[-3],RC[-3])" 'enter sum formula in adjecent cell (Column D)
    Next i
    ws.Range("D2:D" & LastRow).Copy 'copy the formula
    ws.Range("D2:D" & LastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 'paste as values
    Range("C2:C" & LastRow).Delete Shift:=xlToLeft 'delete column C and shift the values from column D to column C
    ws.Range("$A$1:$D$" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlYes 'remove duplicate rows
End Sub

You can use Power Pivot it is a free add-in that you can get and easily activate. 您可以使用Power Pivot,它是一个免费加载项,您可以获取并轻松激活它。 Get & Transform by default in Excel 2016. 默认情况下在Excel 2016中获取和转换

  1. Select any cell in your main table. 选择主表中的任何单元格。
  2. Go to Power Pivot Tab or Data in Excel 2016. 转到Excel 2016中的Power Pivot选项卡或数据
  3. Select Form Table/Range -> OK 选择表单表/范围 -> 确定

It will open the Query Editor there: 它将在此处打开查询编辑器

  1. Select the columns [Date] and [Description] 选择[Date][Description]
  2. Go to Home tab and select Group by 转到首页标签,然后选择分组依据
  3. Put a name for the new field. 为新字段输入名称。
  4. Select the operation. 选择操作。
  5. Select the column to Sum. 选择要加和的列。
  6. OK
  7. Go Home tab select Close & Load 转到首页选项卡,选择关闭并加载
  8. Play with your new data! 玩弄您的新数据!

在此处输入图片说明

You can connect many sources to Power Query and it will be a right click Refresh and you will have your process done! 您可以将许多源连接到Power Query ,只需右键单击“ 刷新”即可完成您的过程!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM