简体   繁体   English

重新排列数据的宏

[英]macro to re-arrange data

I have been trying to write a macro to re-arrange the Cells in the rows and columns of Stock tables for the output I desire.我一直在尝试编写一个宏来为我想要的 output 重新排列 Stock 表的行和列中的单元格。 Luckily the Stock Tables are generally the same each and every time (Different names and values), and the desired outcome is the same format..幸运的是,库存表通常每次都是相同的(不同的名称和值),并且期望的结果是相同的格式。

Here is some example Data.这是一些示例数据。

      A                        

1 Name 1 名称
2 description 2 描述
3 description 3 描述
4 description 4 说明
5 description 5 描述
6 ID#: 56284 6 身份证号:56284
7 Quantity in stock: 34 7 库存数量:34
8 Zoom In and Configure 8 放大和配置

      B

1 Name 1 名称
2 description 2 描述
3 description 3 描述
4 description 4 说明
5 description 5 描述
6 ID#: 56284 6 身份证号:56284
7 Quantity in stock: 50 7 库存数量:50
8 Zoom In and Configure 8 放大和配置

And I would like the Output to go into something like this(If possible to sheet2 starting on Cell B2):我希望 Output 到 go 变成这样的(如果可能的话,从单元格 B2 开始的 sheet2):

B    C    E

B Being Row 1 C being Row 2 3 4 and 5 Combined E being JUST Row 7 Stock Value IE 50 B 是第 1 行 C 是第 2 行 3 4 和 5 组合 E 是第 7 行股票价值 IE 50

On a single spreadsheet there would be 4 columns, and 8 rows I would have to re-arrange.. Making 32 total.在一个电子表格上,我必须重新排列 4 列和 8 行。总共 32 行。

It would be great to automated this, so any help would be greatly appreciated.将其自动化会很棒,因此将不胜感激任何帮助。

Let me clarify my understanding.让我澄清一下我的理解。 For each column you want the following data format:对于每一列,您需要以下数据格式:

    A                                              A
1   Name                                       1   Name
2   Desc1                                      2   Desc1; Desc2; Desc3; Desc4
3   Desc2                      On sheet 2      3   50
4   Desc3                   --------------->
5   Desc4
6   Id#: 56284
7   Quantity in Stock: 50
8   Zoom in and configure

If this is the case you can use the following code.如果是这种情况,您可以使用以下代码。 It assumes your data is in A1 to D8 in Sheet 1.它假设您的数据位于工作表 1 的 A1 到 D8 中。

Sub FormatData()
    Dim col As Integer

    For col = 1 To 4
        With Worksheets(2)
           .Cells(1, col) = Cells(1, col) //Get name
           .Cells(2, col) = Cells(2, col) & "; " & Cells(3, col) & "; " & Cells(4, col) & "; " & Cells(5, col) //Concatenate descriptions into single string
           .Cells(3, col) = Cells(7, col) //Get quantity in stock
        End With
    Next col
End Sub

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

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