简体   繁体   English

文本到多列的列-Excel VBA

[英]Text to columns for multiple columns - Excel VBA

I have many columns of concatenated data that I would like to split by spaces. 我有许多串联的数据列,我想按空格分开。

So from this: 所以从这个:

在此处输入图片说明

To this: 对此:

在此处输入图片说明

This VBA code is very close, 这个VBA程式码非常接近,

    Sub TextToColumns()

'Deines Last Row
    Dim LastRow As Long
    LastRow = 1048576 'the last row possible in excel
    'optional alternative **LastRow** Code
       'Counts number of rows (counts from last row of Column A):
         'Dim LastRow As Long
         'LastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Counts number of Columns (my headers start in row 1)
    Dim LastColumn As Long
    LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column

'Loops Text to columns
    Dim StartingRow, StartingColumn As Long
    StartingRow = 1

    For StartingColumn = 1 To LastColumn
        Range(Cells(StartingRow, StartingColumn), Cells(LastRow, StartingColumn)).Select

        Selection.TextToColumns , DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True

    Next

End Sub

but I would like to use it only on the selected cells, and it overwrites the data to give this: 但我只想在选定的单元格上使用它,它会覆盖数据以提供此信息:

在此处输入图片说明

How can I avoid overwriting the data, and only run the macro on selected cells? 如何避免覆盖数据,而仅在选定的单元格上运行宏? Thank you very much. 非常感谢你。

Try this code. 试试这个代码。 Basicly what it does is that it loops throug the selected rows and merge all the text in each sell of the column into a string, then it splits it up into each cell in the column with space as a delimiter. 基本上,它的作用是循环遍历选定的行,并将列的每次销售中的所有文本合并为一个字符串,然后将其拆分为列中的每个单元格,并以空格作为分隔符。

Remember to select some rows before running the macro. 在运行宏之前,请记住选择一些行。

Sub TextToColumns()

'Counts number of Columns (my headers start in row 1)
    Dim LastColumn As Long
    LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column


'Full strig
    Dim FullString As Variant
'Split string
    Dim SplitString As Variant


'Loops Text to columns

   Dim rng As Range
   Dim lRowSelected As Long
   For Each rng In Selection.Rows

    RowsSelected = rng.Row


        'Making one string from all the cells in the row
        For StartingColumn = 1 To LastColumn

        If StartingColumn = 1 Then

        FullString = Cells(RowsSelected, StartingColumn).Value

        Else

        FullString = FullString & " " & Cells(RowsSelected, StartingColumn).Value
        End If


        Next StartingColumn

            'Splits the string up into each cell with space as a delimiter
            SplitString = Split(FullString, " ")

            For i = 0 To UBound(SplitString)
                Cells(RowsSelected, i + 1).Value = SplitString(i)
                Next i

   Next rng


End Sub

I would 我会

  • concatenate your original rows into one, with a space delimiter 用空格定界符将原始行合并为一
  • and then split that result on the space. 然后在空间上拆分该结果。

The code below gives you the results you show in your to this: screenshot from your original data. 以下代码为您显示了以下结果原始数据的屏幕截图。


Option Explicit
Sub splitMultipleColumns()
    Dim wsSrc As Worksheet, rSrc As Range, rDest As Range
    Dim vSrc As Variant
    Dim vConcat As Variant
    Dim I As Long, J As Long

'Many ways to do this
Set wsSrc = Worksheets("sheet1")
Set rSrc = wsSrc.Cells(1, 1).CurrentRegion

'put results below original, but they could go anyplace
Set rDest = rSrc.Offset(rSrc.Rows.Count + 2).Resize(columnsize:=1)

vSrc = rSrc 'read into array for processing speed

'create array of concatenated rows
ReDim vConcat(1 To UBound(vSrc, 1), 1 To 1)
For I = 1 To UBound(vSrc, 1)
    For J = 1 To UBound(vSrc, 2)
        vConcat(I, 1) = vConcat(I, 1) & " " & vSrc(I, J)
    Next J
    vConcat(I, 1) = Trim(vConcat(I, 1))
Next I

Application.ScreenUpdating = False

rDest.EntireRow.Clear
rDest = vConcat
rDest.TextToColumns DataType:=xlDelimited, consecutivedelimiter:=True, _
    Tab:=False, semicolon:=False, comma:=False, Space:=True, other:=False

'Fix the Header row
Set rDest = rDest.CurrentRegion
With rDest
    For J = .Columns.Count To 4 Step -1
        If .Item(1, J) <> "" Then
            Range(rDest(1, J), rDest(1, J + 1)).Insert (xlShiftToRight)
        End If
    Next J
    rDest.Style = "Output"
End With

End Sub

在此处输入图片说明

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

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