【问题标题】:VBA - Copy a row multiple times based on the number of occurrence of a field in a different tableVBA - 根据不同表中字段的出现次数多次复制行
【发布时间】:2017-01-21 08:46:22
【问题描述】:

我对在 VBA 代码中实现的逻辑感到困惑。我在两个不同的工作表中有两个表,我想根据列将它们连接起来,这将在两个工作表中出现多次。

我附上了我需要的截图。请帮我! 提前致谢。 我正在尝试在这里复制表格,不确定这是否会好起来。

>|===================|====================|===========================|
>| Table 1           |     Table 2        |        Result             |
>|===================|====================|===========================| 
>| Fruit  |  Store   |   Fruit  | Buyer   | Buyer | Fruit  |    Store |
>|===================|====================|===========================|
>| Apples | Walmart  |  Apples  | Rita    | Rita  | Apples |Walmart   |
>--------------------|--------------------|---------------------------|
>| Apples | Target   |  Grapes  | Rita    | Rita  | Apples |Target    |
>--------------------|--------------------|---------------------------|
>| Grapes | Walmart  |  Grapes  | Peter   | Rita  | Grapes |Walmart   |
>--------------------|--------------------|---------------------------|
>| Grapes | Target   |  Oranges | Rita    | Rita  | Grapes |Target    |
>--------------------|--------------------|---------------------------|
>| Oranges| Walmart  |  Oranges | Peter   | Rita  | Oranges|Walmart   |
>--------------------|--------------------|---------------------------|
>| Oranges| Target   |  Oranges | Tom     | Rita  | Oranges|Target    |
>|===================|====================|---------------------------|
>                                         | Peter | Grapes |Walmart   |
>                                         |---------------------------|  
>                                         | Peter | Grapes |Target    |
>                                         |---------------------------| 
>                                         | Peter | Oranges|Walmart   |
>                                         |---------------------------| 
>                                         | Peter | Oranges|Target    |
>                                         |---------------------------| 
>                                         | Tom   | Oranges|Walmart   |
>                                         |---------------------------| 
>                                         | Tom   | Oranges|Target    |
>                                         |===========================| 
>

截图:

【问题讨论】:

  • 你能添加一个描述你到目前为止所尝试的内容吗?
  • 欢迎来到 S.O!你试过什么吗?如果是这样,请提供代码,查看tourhow to ask。友情提示:StackOverflow 不是“我们为您编码”的服务提供商。 Introduction to VBA
  • 您提出的是一个完美的关系数据库应用程序。您觉得需要在 Excel VBA 而不是 MS-Access 或其他 RDB 程序中执行此操作有什么特别的原因吗?
  • This seems very similar 你想要达到的目标。当然这是函数,但想法是一样的。

标签: vba excel


【解决方案1】:

睡了几个小时很快就解决了这个问题,似乎工作正常。

Sub Combine()

    Set wsSrc = Worksheets("Sheet1") 'Populate with source sheet used
    'may need to add a second ws and change array reading below
    Set wsDest = Worksheets("Sheet2") 'populate with where you want data to go
    'may want to change to a listobject and the writes below accordingly
    iDestRow = 2

'Read both tables into arrays
    aTable1 = wsSrc.ListObjects("Table1").DataBodyRange
    aTable2 = wsSrc.ListObjects("Table2").DataBodyRange

'Cycle through each buyer and populate new dataset
    For i = LBound(aTable2) To UBound(aTable2)
        sBuyer = aTable2(i, 2)
        sFruit = aTable2(i, 1)
        For j = LBound(aTable1) To UBound(aTable1)
            'if the buyer exists in table 2 and table 1 populate destination
            If aTable1(j, 1) = sFruit Then
                wsDest.Cells(iDestRow, 2) = sBuyer
                wsDest.Cells(iDestRow, 3) = sFruit
                wsDest.Cells(iDestRow, 4) = aTable1(j, 2)
                iDestRow = iDestRow + 1
            End If
        Next j

    Next i

End Sub

【讨论】:

  • 嗨,我用这个想法运行了两个 for 循环并将内容放在目标上以满足我的需求。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多