-1

我不是一个程序员。我被分配了重新组织 xml 文件的任务,该文件包含 7,000 个产品,每个产品有 60 个属性。该文件需要重新组织,使 60 个属性成为标题,产品及其属性列在它们的下方。我有一些代码可以做到这一点,但它并不完全有效(实际上它似乎有效,它只是永远不会“完成”)。

    Sub Autosort()
Dim x As Long
Dim CL As Range
With Sheets("sheet1")
    For Each CL In Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            Select Case CL.Value
                Case "description"
                .Range("C" & (.Cells(.Rows.Count, "E").End(xlUp).Row + 1)).Value = CL.Offset(, 1).Value
                Case "origin-zip"
                .Range("D" & (.Cells(.Rows.Count, "E").End(xlUp).Row + 1)).Value = CL.Offset(, 1).Value
                Case "name"
                .Range("E" & (.Cells(.Rows.Count, "E").End(xlUp).Row + 1)).Value = CL.Offset(, 1).Value
                Case "download"
                .Range("F" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "yahoo-shopping-category"
                .Range("G" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "abstract"
                .Range("H" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "product-size"
                .Range("I" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "cross-sell"
                .Range("J" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "label"
                .Range("K" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "free-shipping"
                .Range("L" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "multi-box-weights"
                .Range("M" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "orderable"
                .Range("N" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "product-url"
                .Range("O" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "taxable"
                .Range("P" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "adwords_grouping"
                .Range("Q" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "availability"
                .Range("R" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "headline"
                .Range("S" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "markup"
                .Range("T" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "size"
                .Range("U" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "keywords"
                .Range("V" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset"
                .Range("W" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "code"
                .Range("X" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "multi-box-dimensions"
                .Range("Y" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "icon"
                .Range("Z" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "free-shipping-method"
                .Range("AA" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "dimensions"
                .Range("AB" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "caption"
                .Range("AC" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "gender"
                .Range("AD" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "hide-contents"
                .Range("AE" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "adwords_labels"
                .Range("AF" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "brand"
                .Range("AG" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "vwd-enable-multi-cart"
                .Range("AH" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "global-free-ship-exclusion"
                .Range("AI" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "age-group"
                .Range("AJ" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "number-of-boxes"
                .Range("AK" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "condition"
                .Range("AL" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "sale-price"
                .Range("AM" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "color"
                .Range("AN" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "multi-add"
                .Range("AO" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "exclude-states"
                .Range("AP" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "flat-ship-rates"
                .Range("AQ" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset-1"
                .Range("AR" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "ysw-alt-tag"
                .Range("AS" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset-2"
                .Range("AT" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset-3"
                .Range("AU" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset-4"
                .Range("AV" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "inset-5"
                .Range("AW" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "invalid-ship-methods"
                .Range("AX" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "map-price"
                .Range("AY" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "new-icon"
                .Range("AZ" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "ship-alone"
                .Range("BA" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "ypath"
                .Range("BB" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "sale-icon"
                .Range("BC" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "image"
                .Range("BD" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "ship-weight"
                .Range("BE" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "page-title"
                .Range("BF" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "gift-certificate"
                .Range("BG" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "flat-ship-rate"
                .Range("BH" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "exclude-countries"
                .Range("BI" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "google-based-property-type"
                .Range("BJ" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case "price"
                .Range("BK" & (.Cells(.Rows.Count, "E").End(xlUp).Row)).Value = CL.Offset(, 1).Value
                Case Else

            End Select
Bail:
    Next CL
.Columns.AutoFit
End With
End Sub

如果您需要数据样本,请告诉我,我很乐意通过电子邮件发送一个片段。提前致谢。

4

1 回答 1

0

这就是我将如何做到的。在 A1:D1 中制作列标题(属性、值、ID、唯一)。在C2中,输入这个公式并填写

=IF(A2="description",SUM(C1)+1,C1)

它假定“描述”是每个项目的第一个属性。这将为每个项目提供一个标识符。在 D2 中输入这个公式并填写

=ROW()

这将为每一行提供一个唯一的编号。现在从数据创建一个数据透视表。

Column Labels = Attribute
Row Labels = OD
Values = Sum of Unique

在数据透视表下方,将此公式放入 B7005(或数据下方的任何行)

=B4

并填充到数据透视表的范围内。在 B7006 中,把这个公式

=INDEX(OriginalSheet!$B$2:$B$50000,MATCH(B5,OriginalSheet!$D$2:$D$50000,FALSE),1)

并根据您的需要向右和向下填写尽可能多的数据。最后选择以 B7005 开头的范围,将特殊值复制并粘贴到自身顶部。

如果你必须不止一次这样做,或者你真的想使用代码,那么我会使用这样的东西

    Sub TransposeXml()

    Dim i As Long
    Dim sh As Worksheet
    Dim rNext As Range

    Set sh = ThisWorkbook.Worksheets.Add

    'assumes data starts in row 2 and you have 61 attributes
    'and that attributes are in the same order
    For i = 2 To Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row Step 61
        Set rNext = sh.Cells(sh.Rows.Count, 1).End(xlUp).Offset(1, 0)
        Sheet1.Cells(i, 2).Resize(61, 1).Copy
        rNext.PasteSpecial xlPasteAll, , , True
    Next i

End Sub
于 2013-10-21T16:17:38.703 回答