可以将行数据转换为特定列格式的宏

时间:2016-01-28 07:40:52

标签: excel vba excel-vba

我需要编写一个可以将数据转换为具有特定格式的新工作表的宏。

数据可以粘贴到工作表1中,一旦宏运行,它应该按照工作表2中的格式正确转置数据(包括单元格大小,字体等)我创建了工作表的样本,但不知道如何编码。

非常感谢任何帮助。您可以从以下链接下载文件:

https://drive.google.com/file/d/0By5ixqk5woW4LVhfMnFPcmNsZDA/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

这应该是你想要做的一个非常好的开始。我把你所附的图片作为例子。所以我尊重专栏等......

Option Explicit
Sub test()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long
Dim i As Long, j As Long, a As Long

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1") 'Change the name of your sheet where there is all data
Set ws2 = wb.Sheets("Sheet2") 'Change the name of your sheet where you want to past

Lastrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

j = 1 ' Start to column A
a = 1 ' Start to row 1

For i = 2 To Lastrow

        ws1.Cells(i, 1).Copy 'ItemCode
        ws2.Cells(a, j).PasteSpecial (xlPasteAll)

        ws1.Cells(i, 2).Copy ' ItemSku
        ws2.Cells(a + 1, j).PasteSpecial (xlPasteAll)

        ws1.Cells(i, 4).Copy ' VendorSkuCode
        ws2.Cells(a + 2, j).PasteSpecial (xlPasteAll)

        ws1.Cells(i, 5).Copy 'ItemSize
        ws2.Cells(a + 3, j).PasteSpecial (xlPasteAll)

        ws1.Cells(i, 6).Copy 'ItemColor
        ws2.Cells(a + 4, j).PasteSpecial (xlPasteAll)

        ws1.Cells(i, 7).Copy 'mrp
        ws2.Cells(a + 5, j).PasteSpecial (xlPasteAll)

        ws2.Cells(a + 6, j) = "*" & ws1.Cells(i, 1) & "*" 'ItemCode

     j = j + 2 ' Go to 2 columns on the right

     If j > 7 Then ' If j is in Column G then
        a = a + 8 ' Go to 9 row below
        j = 1 ' Reset the column to column A
    End If


Next i

End Sub