【问题标题】:How to Split text and loop to next row如何拆分文本并循环到下一行
【发布时间】:2019-02-06 17:41:25
【问题描述】:

我正在使用 split 函数来使用空格分割文本。我已经让我的宏来拆分文本,但我很难让循环移动到下面的下一行进行拆分。

Sub Split_Text_Test1()

Dim Txt As String
Dim i As Integer
Dim FullName As Variant

Range("A1").Select

Txt = ActiveCell.Value

FullName = Split(Txt, " ")

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

End Sub

【问题讨论】:

    标签: excel vba loops split


    【解决方案1】:

    当您从 A1 开始时,您可能需要更改循环内的位。这假设您想要 A2 及以下的条目。通常不建议使用 Select/Activate,不是很健壮。

    编辑为跨列而不是向下移动。

    For i = 0 To UBound(FullName)
        Range("A1").Offset(,i + 1).Value = FullName(i)
    Next i
    

    也就是说,您可以完全避免循环并使用

    Range("B1").Resize(, UBound(FullName) + 1).Value = FullName
    

    【讨论】:

    • 您好 SJR,感谢您回答我的问题。当我运行它时,它会将我的文本分成不同的行。理想情况下,我希望将 A 列文本拆分为 A、B 和 C 列,然后让宏转到下一行以执行相同的操作。
    • 只需在 Offset 前加一个逗号(上面已修改)以偏移列而不是行。但是,为什么不使用 Text to Columns,效率更高呢?
    • SJR 你能解释一下你的意思是文本到列吗?您是指列中的公式吗?
    • 不,这是数据菜单中的一个选项。点击几下即可完成excel-easy.com/examples/text-to-columns.html
    • 了解更多背景知识 - 我创建的代码可以打开一个文本文件并从该文本文件复制数据,并在每次运行时将其粘贴到此工作簿的单元格 A1 中。我刚刚使用了 Text to Columns 功能,它可以按我的意愿工作,但是当我运行宏来提取新数据时,Text to Columns 会被清除。
    【解决方案2】:

    在这种情况下,我将使用循环(您的解决方案与此相差不远):

    Dim Txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim R As Integer, C As Integer, MaxR as Integer
    C = 1 ' can be another loop as well
    For R = 1 to 1000
    
    Txt = Trim(Cells(r,1).Value) ' will remove space from start and end
    FullName = Split(Txt, " ")
    
    For i = 0 To UBound(FullName)
        Cells(R , C + 1 + i ).Value = FullName(i)
    Next i
    
    Next R
    

    【讨论】:

    • 请注意,行计算变量应始终为 Long 类型,因为 Excel 的行数超过了 Integer 可以处理的 Dim R As Long。我推荐在 VBA 中使用 always to use Long instead of Integer,因为在 Integer 中根本没有任何好处。 • MaxR 已声明但从未使用过。
    【解决方案3】:
    Sub Split_Text_Test1()
    
    Dim Txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim R As Integer, C As Integer
    Range("A1").Select ' assumes that the cells below that are empty 
    
    Txt = ActiveCell.Value
    
    FullName = Split(Txt, " ")
    R = ActiveCell.Row
    C = ActiveCell.Column
    For i = 0 To UBound(FullName)
        Cells(R + 1 + i, C).Value = FullName(i)
    Next i
    
    End Sub
    

    【讨论】:

    • 您好,Wilhelm,感谢您回答我的问题。当我运行它时,它会将我的文本分成不同的行。理想情况下,我希望将 A 列文本拆分为 A、B 和 C 列,然后让宏转到下一行以执行相同的操作。
    • 另外,我要拆分的文本看起来都类似于“14912E33 190205 099.7920”。它最后有 55 个空格,我认为这也会让我的代码失效。我有一个宏可以从 .TXT 文件中复制此文本,这就是它看起来很傻的原因。
    【解决方案4】:

    我在您的代码中添加了一些东西,看看这是否符合您的目的。然而,正如 SJR 所说,“数据”菜单中的“文本到列”选项可以用更少的精力做同样的事情。

    Sub Split_Text_Test1()
    
    Dim Txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim lastRow As Long
    Dim myRange As Range
    
    With ActiveSheet.UsedRange
            lastRow = .Rows(.Rows.Count).Row
    End With
    Debug.Print lastRow
    
    'Range("A1").Select
    
    Set myRange = ActiveSheet.Range("A1", "A" & lastRow)
    
        For Each cell In myRange
    
            Txt = cell.Value
    
            FullName = Split(Txt, " ")
    
                For i = 0 To UBound(FullName)
                    Cells(cell.Row, i + 1).Value = FullName(i)
                Next i
        Next cell
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-19
      • 2016-02-22
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2018-08-04
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多