将一个excel单元格中的特定信息拆分为其他几个

时间:2014-12-03 16:10:28

标签: excel excel-formula

我需要找到一种方法来分割excel上的一些数据:例如 如果单元格中包含以下内容:LWPO0001653/1654/1742/1876/241 /之后的所有信息都应为LWPO000...,并带有该号码。

无论如何将它们分开并加入LWPO000 in?所以他们出来LWPO0001653 LWPO0001654 等等

我可以手动做,但我有数千人这样做会花费很长时间。

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

以下是使用Excel公式的解决方案。

在A1中使用原始字符串,假设前七个字符是重复的字符,那么:

B1:  =LEFT($A1,FIND("/",$A1)-1)
C1:  =IF(LEN($A1)-LEN(SUBSTITUTE($A1,"/",""))< COLUMNS($A:A),"",LEFT($A1,7)&TRIM(MID(SUBSTITUTE(MID($A1,8,99),"/",REPT(" ",99)),(COLUMNS($A:A))*99,99)))

选择C1并根据需要向右填充。然后从第1行填写

编辑:对于VBA解决方案,请尝试使用此代码。它假定源数据在A列中,并将结果放在B列的相邻位置(如有必要,可以轻松更改)。它可以在VBA中使用数组,因为执行多个工作表读/写会降低速度。它会在不同的单元格中处理不同数量的分割,但如果我们知道分割的数量总是相同的话,可以缩短它们。


Option Explicit
Sub SplitSlash()
    Dim vSrc As Variant
    Dim rRes As Range, vRes() As Variant
    Dim sFirst7 As String
    Dim V As Variant
    Dim COL As Collection
    Dim I As Long, J As Long
    Dim lMaxColCount As Long

Set rRes = Range("B1")  'Set to A1 to overwrite
vSrc = Range("a1", Cells(Rows.Count, "A").End(xlUp))

'If only a single cell, vSrc won't be an array, so change it
If Not IsArray(vSrc) Then
    ReDim vSrc(1 To 1, 1 To 1)
    vSrc(1, 1) = Range("a1")
End If

'use collection since number of columns can vary
Set COL = New Collection
For I = 1 To UBound(vSrc)
    sFirst7 = Left(vSrc(I, 1), 7)
    V = Split(vSrc(I, 1), "/")
    For J = 1 To UBound(V)
        V(J) = sFirst7 & V(J)
    Next J
    lMaxColCount = IIf(lMaxColCount < UBound(V), UBound(V), lMaxColCount)
    COL.Add V
Next I

'Results array
ReDim vRes(1 To COL.Count, 1 To lMaxColCount + 1)
For I = 1 To UBound(vRes, 1)
    For J = 0 To UBound(COL(I))
        vRes(I, J + 1) = COL(I)(J)
    Next J
Next I

'Write results to sheet
Set rRes = rRes.Resize(UBound(vRes, 1), UBound(vRes, 2))
With rRes
    .EntireColumn.Clear
    .Value = vRes
    .EntireColumn.AutoFit
End With

End Sub

答案 1 :(得分:1)

我显然忽略了这一点:-)但无论如何,在B1中并复制到适合:

=SUBSTITUTE(A1,"/","/"&LEFT(A1,7))  

选择ColumnB,Copy and Paste Special,值顶部 将列文本应用于ColumnB,分隔,并以/作为分隔符。

答案 2 :(得分:0)

有几种方法可以解决这个问题。最快的可能是:

假设数据在A列中:

  1. 突出显示列,转到数据&gt;&gt;文本到列
  2. 选择&#34;分隔&#34;在&#34;其他&#34;框,放/
  3. 点击确定。您将数据拆分为多个单元格
  4. 在B处插入一列并输入公式=Left(A1, 7)
  5. 在公式= Right(A1, Length(A1)-7)
  6. 的C和坑中插入一列
  7. 你现在有B列,前7个字符,B,C,D,E,F等列,最后一点。您可以使用=Concatenate(B1,C1)=Concatenate(B1,D1)等对每个列重新连接值。
  8. 一个快速的VBa,它的功能几乎和@Kevin的一样。我在看到他的回答之前写了这篇文章,我不想丢掉工作;)

    Sub breakUpCell()
        Dim rngInput As Range, rngInputCell As Range
        Dim intColumn As Integer
        Dim arrInput() As String
        Dim strStart As String
        Dim strEnd As Variant
    
        'Set the range for the list of values (Assuming Sheet1 and A1 is the start)
        Set rngInput = Sheet1.Range("A1").Resize(Sheet1.Range("A1").End(xlDown).Row)
    
        'Loop through each cell in the range
        For Each rngInputCell In rngInput
    
            'Split up the values after the first 7 characters using "/" as the delimiter
            arrInput = Split(Right(rngInputCell.Value, Len(rngInputCell.Value) - 7), "/")
    
            'grab the first 7 characters
            strStart = Left(rngInputCell.Value, 7)
    
            'We'll be writing out the values starting in column 2 (B)
            intColumn = 2
    
            'Loop through each split up value and assign to strEnd
            For Each strEnd In arrInput
    
                'Write the concatenated value out starting at column B in the same row as rngInputCell
                Sheet1.Cells(rngInputCell.Row, intColumn).Value = strStart & strEnd
    
                'Head to the next column (C, then D, then E, etc)
                intColumn = intColumn + 1
    
            Next strEnd
    
        Next rngInputCell
    End Sub
    

答案 3 :(得分:0)

以下是使用宏的方法:

这就是发生的事情:

1)设置要处理的范围 2)循环遍历范围内的每个单元格并检查它是否为空白 3)如果单元格包含斜杠字符,则将其拆分并处理 4)跳过第一条记录并将“LWPO000”和当前字符串连接到相邻的单元格。

Sub CreateLWPO()

On Error Resume Next

Application.ScreenUpdating = False

Dim theRange
Dim cellValue
Dim offset As Integer
Dim fields

'set the range of cells to be processed here
Set theRange = range("A1:A50")

'loop through each cell and if not blank process
For Each c In theRange

    offset = 0 'this will be used to offset each item found 1 cell to the right (change this number to this first column to be populated)
    If c.Value <> "" Then
        cellValue = c.Value

        If InStr(cellValue, "/") > 0 Then

          fields = Split(cellValue, "/")

          For i = 1 To UBound(fields)

            offset = offset + 1
            cellValue = "LWPO000" & fields(i)
            'if you need to pad the number of zeros based on length do this and comment the line above
            'cellValue = "LWPO" & Right$(String(7, "0") & fields(i), 7)
            c.offset(0, offset).Value = cellValue

          Next i

        End If

    End If
    Next

Application.ScreenUpdating = True

End Sub