【问题标题】:Macro or other solution in excel to automate interaction of data in two worksheets to come up with a third worksheetexcel中的宏或其他解决方案可自动交互两个工作表中的数据以产生第三个工作表
【发布时间】:2015-06-18 07:36:57
【问题描述】:
【问题讨论】:
标签:
excel
google-sheets
vba
【解决方案1】:
对于公司,您有一个简单的键/值组合,因此如果在 [Balance] 中您有公司名称并且想要显示 [Companies] 代码表中的公司代码,您可以使用简单的 VLOOKUP,例如
F2: =vlookup(D2,Companies!A:B,2,false)
(注意:通常它会是反向的...您有代码并想用名称扩展...在这种情况下,您必须反转 [Companies] 中的列顺序,因为键列必须去在第一列。)
预算组的情况不是很清楚,除了所有三列的组合之外,此列表可能没有唯一键。
【解决方案2】:
我在Excel vba to create every possible combination of a Range 找到了Spioter 提供的解决方案,事实证明它非常有帮助!修改了脚本以适合我的需要和不反映问题中的 Google 表格链接的表格。谢谢Spioter!
Sub sub_CrossJoin()
Worksheets("Combinations").Range("A4:B2000").ClearContents
Dim rg_Legend As Range
Dim rg_Implementers As Range
Dim rg_RowLegend As Range
Dim rg_RowImplementers As Range
Dim rg_DestinationCol As Range
Dim rg_DestinationCell As Range
Dim int_PriorCombos As Long
Dim int_TotalCombos As Long
Dim int_LegendRowCount As Long
Dim int_ImplementersRowCount As Long
Dim int_ValueRepeats As Long
Dim int_ValueRepeater As Long
Dim int_ValueCycles As Long
Dim int_ValueCycler As Long
int_TotalCombos = 1
int_PriorCombos = 1
int_ValueRowCount = 0
int_ValueCycler = 0
int_ValueRepeater = 0
Set rg_Legend = Worksheets("Legend").Range("A8:A500")
Set rg_Implementers = Worksheets("Implementers").Range("A3:A50")
Set rg_DestinationCol = Worksheets("Combinations").Range("A4")
'Get total combos
int_LegendRowCount = 0
For Each rg_RowLegend In rg_Legend.Cells
If rg_RowLegend.Value = "" Then
Exit For
End If
int_LegendRowCount = int_LegendRowCount + 1
Next rg_RowLegend
int_TotalCombos = int_TotalCombos * int_LegendRowCount
int_ImplementersRowCount = 0
For Each rg_RowImplementers In rg_Implementers.Cells
If rg_RowImplementers.Value = "" Then
Exit For
End If
int_ImplementersRowCount = int_ImplementersRowCount + 1
Next rg_RowImplementers
int_TotalCombos = int_TotalCombos * int_ImplementersRowCount
int_LegendRowCount = 0
int_ImplementersRowCount = 0
'Calculate the repeats needed for each row value and then populate the destination
int_LegendRowCount = 0
For Each rg_RowLegend In rg_Legend.Cells
If rg_RowLegend.Value = "" Then
Exit For
End If
int_LegendRowCount = int_LegendRowCount + 1
Next rg_RowLegend
int_PriorCombos = int_PriorCombos * int_LegendRowCount
int_ValueRepeats = int_TotalCombos / int_PriorCombos
int_ValueCycles = (int_TotalCombos / int_ValueRepeats) / int_LegendRowCount
int_ValueCycler = 0
int_ValueRepeater = 0
Set rg_DestinationCell = rg_DestinationCol
For int_ValueCycler = 1 To int_ValueCycles
For Each rg_RowLegend In rg_Legend.Cells
If rg_RowLegend.Value = "" Then
Exit For
End If
For int_ValueRepeater = 1 To int_ValueRepeats
rg_DestinationCell.Value = rg_RowLegend.Value
Set rg_DestinationCell = rg_DestinationCell.Offset(1, 0)
Next int_ValueRepeater
Next rg_RowLegend
Next int_ValueCycler
Set rg_DestinationCol = rg_DestinationCol.Offset(0, 1)
For Each rg_RowImplementers In rg_Implementers.Cells
If rg_RowImplementers.Value = "" Then
Exit For
End If
int_ImplementersRowCount = int_ImplementersRowCount + 1
Next rg_RowImplementers
int_PriorCombos = int_PriorCombos * int_ImplementersRowCount
int_ValueRepeats = int_TotalCombos / int_PriorCombos
int_ValueCycles = (int_TotalCombos / int_ValueRepeats) / int_ImplementersRowCount
int_ValueCycler = 0
int_ValueRepeater = 0
Set rg_DestinationCell = rg_DestinationCol
For int_ValueCycler = 1 To int_ValueCycles
For Each rg_RowImplementers In rg_Implementers.Cells
If rg_RowImplementers.Value = "" Then
Exit For
End If
For int_ValueRepeater = 1 To int_ValueRepeats
rg_DestinationCell.Value = rg_RowImplementers.Value
Set rg_DestinationCell = rg_DestinationCell.Offset(1, 0)
Next int_ValueRepeater
Next rg_RowImplementers
Next int_ValueCycler
Set rg_DestinationCol = rg_DestinationCol.Offset(0, 1)
End Sub