2016-09-13 1 views
0

VBAコードで実装するロジックが残っています。私は2つの異なるワークシートに2つのテーブルを持っています&両方のシートに複数回出現する列に基づいてそれらを結合したいと思います。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 | 
>           |===========================| 
> 

スクリーンショット:数時間の睡眠に迅速にこれをHしちゃっ

Screenshot

+2

これまでに試したことの説明を追加できますか? – slayernoah

+0

S.Oへようこそ!何か試しましたか?もしそうなら、コードを提供して、[ツアー](http://stackoverflow.com/tour)と[質問する](http://stackoverflow.com/help/how-to-ask)を見てください。 )。フレンドリーリマインダー:StackOverflowは "あなたのためのコード"サービスプロバイダーではありません。 [VBA入門](https://blog.udemy.com/excel-macros-tutorial/) – Sgdva

+0

あなたが提案しているのは、リレーショナルデータベースのための完璧なアプリケーションです。 MS-Accessや他のRDBプログラムの代わりにExcel VBAでこれを行う必要があると思われる特別な理由はありますか? – SandPiper

答えて

0

は、正常に動作するように見えました。

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 
+0

こんにちは、私は2つのforループを実行し、目的地にコンテンツを配置して自分のニーズに到達するというアイデアを使用しました。どうもありがとう! – user6827365

関連する問題