如何将字符串拆分为多个单元格的单元格? [英] How to split string into cells for multiple cells?

查看:102
本文介绍了如何将字符串拆分为多个单元格的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码遍历包含名称的单元格列表,并将其拆分为原始单元格旁边的单元格.我首先需要一些基本的代码,但是我一直在努力使其遍历列表的其余部分,并且将其输出到原始文件的旁边,而不是像现在这样在A1中输出.我认为这是代码的单元格"部分存在的问题,但我无法完全解决.

I want my code to go through a list of cells containing names and split them up into the cells next to the original. I have some basic code to do the first bit, but I'm struggling to get it to cycle through the rest of my list, and also outputting it next to the original rather than in A1 as it does currently. I presume it's an issue with the 'Cell' part of the code but I can't quite fix it.

Sub NameSplit()

    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim x As String, cell As Range

    txt = ActiveCell.Value

    FullName = Split(txt, " ")

    For i = 0 To UBound(FullName)

        Cells(1, i + 1).Value = FullName(i)

    Next i


End Sub

推荐答案

在名称值范围内使用For Each循环.在这种情况下,我只是假设它们在第一列中,但是您可以相应地进行调整:

Use a For Each loop on the range of name values. In this case, I just assumed they were in the first column but you can adjust accordingly:

Sub NameSplit()

Dim txt As String
Dim i As Integer
Dim FullName As Variant
Dim x As String, cell As Range

For Each cell In ActiveSheet.Range(Cells(1,1),Cells(ActiveSheet.UsedRange.Count,1))
     txt = cell.Value

     FullName = Split(txt, " ")

     For i = 0 To UBound(FullName)

         cell.offset(0,i + 1).Value = FullName(i)

     Next i

Next cell

End Sub

这篇关于如何将字符串拆分为多个单元格的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆