MS Excel生成子表/智能分组

hgncfbus  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(187)

我在MS Excel(2016)中有2个数据表,需要分析2张表中的数据。下面是我需要的示意图问题:
()- sheet1 - main

---------------------------
| id | product | manuf | q | 
---------------------------
| 001| prt_1   | man_1 |150|
---------------------------
| 002| prt_2   | man_2 |800|

字符串
()- sheet2 - submain

--------------------
|id | date | prices|
--------------------
|001|17.01 | 120   |
--------------------
|001|16.02 | 99    |
--------------------
|002|17.03 | 110   |
--------------------
|002|15.02 | 10    |


()-我想做的是把它们像这样分组

---------------------------
| id | product | manuf | q |
---------------------------
- | 001| prt_1   | man_1 |150|
  ----------------------------
   |001|17.01 | 120   |
   --------------------
   |001|16.02 | 99    |
   
---------------------------
+ | 002| prt_2   | man_2 |800|


换句话说,它是MS Access SubDataSheet的模拟,我知道如何在Access中完成,但需要在Excel中完成。我尝试了Pivot Table和Power Pivot,但仍然没有运气。
第二个变体相同的任务,但另一个版本的源数据。而不是2张所有数据在一个,但行增加了一倍和三倍。不知道哪个变体更适合做所需的分组。

--------------------------------------------
| id | product | manuf | q |  date | prices|  
--------------------------------------------
|001 | prt_1   | man_1 |150| 17.01 | 120   |
--------------------------------------------
|001 | prt_1   | man_1 |150| 16.02 | 99    |


有没有一些代码来执行这个?需要帮助和建议。

w1e3prcc

w1e3prcc1#

这样的事情

Public Sub Program()
    Dim i As Long
    Dim j As Long
    Dim k As Long
    i = 2
    j = 2
    k = 2

    Do While Worksheets("Sheet1").Cells(i, "A").Value <> ""
        'data from sheet1
        Worksheets("Result").Cells(k, "A").Value = Worksheets("Sheet1").Cells(i, "A").Value
        Worksheets("Result").Cells(k, "B").Value = Worksheets("Sheet1").Cells(i, "B").Value
        Worksheets("Result").Cells(k, "C").Value = Worksheets("Sheet1").Cells(i, "C").Value
        Worksheets("Result").Cells(k, "D").Value = Worksheets("Sheet1").Cells(i, "D").Value

        k = k + 1

        Do While Worksheets("Sheet1").Cells(i, "A").Value = Worksheets("Sheet2").Cells(j, "A").Value
            'data from sheet1
            Worksheets("Result").Cells(k, "A").Value = Worksheets("Sheet2").Cells(j, "A").Value
            Worksheets("Result").Cells(k, "B").Value = Worksheets("Sheet2").Cells(j, "B").Value
            Worksheets("Result").Cells(k, "C").Value = Worksheets("Sheet2").Cells(j, "C").Value
            Worksheets("Result").Cells(k, "D").Value = Worksheets("Sheet2").Cells(j, "D").Value

            k = k + 1
            j = j + 1
        Loop

        k = k + 1
        i = i + 1
    Loop
End Sub

字符串

相关问题