文本到列的宏,然后将转换后的文本转换为列并插入行


0

我有一个包含三列SKU,标题和大小的Excel表格,如下所示:

enter image description here

运行宏后,我需要表单如下所示:

enter image description here

我被困在添加行以将转换后的文本与列匹配。


1
@BruceWayne答案更先进。我试图给你一个答案,给你一些见解,这样你就可以开始编写自己的宏了。
M-M

Answers:


0

这是如何运作的?

Sub splitBySize()
Dim lastRow As Long, i As Long, k As Long
Dim sizes() As String

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

For i = lastRow To 2 Step -1
    sizes = Split(Cells(i, 3), ", ") 'Add sizes to an array
    If UBound(sizes) <> 0 Then ' If there's more than one size, then...
        Range(Cells(i + 1, 1), Cells(i + UBound(sizes), 1)).EntireRow.Insert
        For k = LBound(sizes) To UBound(sizes) ' This will add the sizes to the new cells inserted
            Cells(i, 3).Offset(k, 0).Value = sizes(k)
        Next k
    End If
Next i

End Sub

基本上,它只是查看每一行,将大小放入一个数组,在两者之间添加行,然后用大小填充单元格。


0

试试这个,但更改工作表名称(即 Sheet1 and Sheet2 )到工作簿中的工作表。注意 Sheet2 是一张空纸,所需的结果将存储在其中。

Option Explicit

Dim wshI As Worksheet
Dim wshO As Worksheet
Dim i As Integer
Dim j As Integer
Dim r As Integer

Sub delimited()

Set wshI = Worksheets("Sheet1") 'change this to the sheet that has your data
Set wshO = Worksheets("Sheet2") 'make a new sheet and change this to its name

'This extract each size to a column (text to columns)
    wshI.Activate
 'Change "100" to the last column of your data
  wshI.Range("C2:C100").TextToColumns Destination:=Range("C2"), DataType:=xlDelimited, _
  TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
  Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
  :=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2)), TrailingMinusNumbers:= _
        True

i = 1
j = 3
r = 1
'This put the desired outcome into the sheet2
While wshI.Cells(i, 1) <> ""
    Do While wshI.Cells(i, j) <> ""
        If j = 3 Then
            wshO.Cells(r, 1) = wshI.Cells(i, 1)
            wshO.Cells(r, 2) = wshI.Cells(i, 2)
            wshO.Cells(r, 3) = wshI.Cells(i, 3)
        Else
            wshO.Cells(r, 3) = wshI.Cells(i, j)
        End If
        j = j + 1
        r = r + 1
    Loop
    j = 3
    i = i + 1
Wend

i = 2
j = 4

'This put the data into its original format
While wshI.Cells(i, 1) <> ""
    Do While wshI.Cells(i, j) <> ""
        wshI.Cells(i, 3) = wshI.Cells(i, 3) & ", " & wshI.Cells(i, j)
        wshI.Cells(i, j).Clear
        j = j + 1
    Loop
j = 4
i = i + 1
Wend

End Sub

0

您可以使用数据►获取和转换►从表格

  • 拆分逗号或逗号+空格上的“大小”列
  • 选择SKU和标题列
  • 单独显示“其他”列
  • 删除第三列;并重命名新的第三列“大小”
  • 保存查询
  • 使用条件格式来清空A列和A列中的相应条目。乙
    • CF公式将是`= AND($ A2&lt;&gt; $ A1,$ B2&lt;&gt; $ B1)
    • 自定义数字格式将是 ;;;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.